Can't seem to assign a cookie for permissions

Discussion in 'PHP' started by jawinn, Nov 14, 2006.

  1. #1
    I'm using the code below. The problem is that I can't get the cookie for permissions assigned. The sequence now is that I login, then I get an error message referencing:

    Warning: Cannot modify header information - headers already sent by (output started at xxx/dev2/auth.php:2) in xxx/dev2/auth.php on line 64

    I am then redirected to the appropriate page.

    
    
    <?php
    
    $auth = false;
    
    if (isset($_POST[name]) && isset($_POST[password])) {
    
    	mysql_connect('xlocalserverx', 'xloginx', 'xpasswordx')
    		or die ('Unable to connect to server.');
    
    	mysql_select_db('xdatabasex')
    		or die ('Unable to select database.');
    
    	$sql = "SELECT * FROM auth WHERE name = '$_POST[name]' AND password = '$_POST[password]'";
    
    	$result = mysql_query($sql)
    		or die ('Unable to execute query.');
    
    	$num = mysql_numrows($result);
    
    	if ($num != 0) {
    		$auth = true;
    		}
    		
    	}
    
    if (! $auth) {
    
    	echo "Authorization Failed";
    	exit;
    	}
    
    else if ($auth = true) {
    
    $sql2 = "SELECT ACCESS FROM auth WHERE name = '$_POST[name]'";
    $result2 = mysql_query($sql2);
    $row = mysql_fetch_array($result2, MYSQL_BOTH);
    
    if ($row[0] == "Full")
    	{
    	$auth = "Full";
    	?>
    	<meta http-equiv="refresh" content="2;URL=./full.php">
    	<?php
    	}
    	
    elseif ($row[0] == "View")
    	{
    	?>
    	<meta http-equiv="refresh" content="2;URL=http://www.yahoo.com">
    	<?php
    	}
    	
    elseif ($row[0] == "Useradmin")
    	{
    	$auth = "Useradmin";
    	?>
    	<meta http-equiv="refresh" content="2;URL=./useradmin/useradmin.php">
    	<?php 
    	}
    
    }
    
    setcookie("author", $auth); <!-- LINE 64 -->
    
    ?>
    
    Code (markup):
    Any help is much appreciated.
     
    jawinn, Nov 14, 2006 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    You cannot set cookies after outputting ANYTHING to the browser, including white spaces. Trim all spaces around the <?php wrappers, and place the setcookie() fuction before all echo()s.
     
    nico_swd, Nov 14, 2006 IP
    jawinn likes this.
  3. jawinn

    jawinn Active Member

    Messages:
    1,024
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    88
    #3
    That worked thanks.

    Now I have a second issue. The destination page is not recognizing the cookie. I keep getting my error below, "you must first login" (5th line from the bottom).

    
    <?php
    
    $authlevel = $HTTP_COOKIE_VARS["author"];
    if ($authlevel == "Full")
    { 
    
    require_once('./Connections/sqltest.php'); 
    $maxRows_Recordset1 = 10;
    $pageNum_Recordset1 = 0;
    if (isset($HTTP_GET_VARS['pageNum_Recordset1'])) {
      $pageNum_Recordset1 = $HTTP_GET_VARS['pageNum_Recordset1'];
    }
    $startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;
    
    mysql_select_db($database_sqltest, $sqltest);
    $query_Recordset1 = "SELECT * FROM car";
    $query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
    $Recordset1 = mysql_query($query_limit_Recordset1, $sqltest) or die(mysql_error());
    $row_Recordset1 = mysql_fetch_assoc($Recordset1);
    
    if (isset($HTTP_GET_VARS['totalRows_Recordset1'])) {
      $totalRows_Recordset1 = $HTTP_GET_VARS['totalRows_Recordset1'];
    } else {
      $all_Recordset1 = mysql_query($query_Recordset1);
      $totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
    }
    $totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="javascript">
    <!--
    
    function test()
    {
       if (document.req.id.value =="")
              { alert("Please enter an id number.");
          return false;}
       
          {
          return true;
          }
    }
    
    //-->
    </script>
    
    </head>
    
    <body>
    <p>Click <a href="form.php">here</a> to add a record.</p>
     
    
    <form name="req" method="post" action="delete.php" onSubmit="return test()">
     <p>Delete Record by ID 
       <input type="text" name="id">
       <input type="submit" name="Submit" value="Delete">
     </p>
     <p>&nbsp; </p>
    </form>
    <table border="1">
      <tr>
        <td width="115">year</td>
        <td width="122">make</td>
        <td width="126">model</td>
        <td width="119">miles</td>
        <td width="122">mods</td>
        <td width="119">ID</td>
      </tr>
      <?php do { ?>
      <tr>
        <td><?php echo $row_Recordset1['year']; ?></td>
        <td><?php echo $row_Recordset1['make']; ?></td>
        <td><?php echo $row_Recordset1['model']; ?></td>
        <td><?php echo $row_Recordset1['miles']; ?></td>
        <td><?php echo $row_Recordset1['mods']; ?></td>
    	<td><?php echo $row_Recordset1['id']; ?></td>
      </tr>
      <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
    </table>
    
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <?php
    mysql_free_result($Recordset1);
    }
    else 
    	{
    
    	echo "You must first login.";
    	}
    ?>
    </body>
    </html> 
    
    Code (markup):
     
    jawinn, Nov 14, 2006 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Try $_COOKIE['author'] instead of $HTTP_COOKIE_VARS["author"].
     
    nico_swd, Nov 14, 2006 IP
  5. jawinn

    jawinn Active Member

    Messages:
    1,024
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    88
    #5
    Didn't work. Any other ideas?
     
    jawinn, Nov 14, 2006 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Where did you place the setcookie() now? It should go here.

    
    if ($row[0] == "Full")
    {     
            $auth = "Full";
            setcookie("author", $auth);
    ?>
    <meta http-equiv="refresh" content="2;URL=./full.php">
    <?php
    }
    
    PHP:
     
    nico_swd, Nov 14, 2006 IP