SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Cookie Problem

  1. #1
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cookie Problem

    I've been using cookies for a while now, and i've come across a problem with them. I've started to make a user authentication system, where i use the "header ("Location: );" method to redirect users and non-users.

    I've only just started this, and i've uploaded what i've done so you can see for your self that its just not working. I would like to know why

    Heres the code that makes: $error = true, you dont need to read it, because i know it makes $error = true, but if you read on you will inderstand, this file is called "config.php"

    Code:
    <?
    // Edit these details
    $mysql['host'] = "localhost"; // Your Host for your mySQL account.
    $mysql['username'] = "admin"; // Your Username for your mySQL account.
    $mysql['password'] = "password"; // Your Password for your mySQL account.
    $mysql['dbname'] = "xsig"; // Your database name for your mySQL account.
    $site = ""; // Enter your site details here, eg. ".sigx.co.uk"
    
    // End edit zone
    
    function selfURL() 
    { 
    	$s = empty($_SERVER["HTTPS"]) ? '' 
      : ($_SERVER["HTTPS"] == "on") ? "s" 
      : ""; 
    	$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s; 
    	$port = ($_SERVER["SERVER_PORT"] == "80") ? "" 
      : (":".$_SERVER["SERVER_PORT"]); 
    	return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI']; 
    } 
    function strleft($s1, $s2) 
    { 
    	return substr($s1, 0, strpos($s1, $s2)); 
    }
    
    $ref = $_SERVER['HTTP_REFERER'];
    // Next code checks the database.
    $link = mysql_connect($sql['host'], $sql['username'], $sql['password']);
    if ($link = false)
    {
    	$error = true;
    	$error_details['number'] = 1;
    	$error_details['text'] = "Cannot connect to host";
    	setcookie("xsig-number", $error_details['number'], time()+60);
    	setcookie("xsig-text", $error_details['text'], time()+60);
    } else {
    	$db = mysql_select_db($mysql['dbname']);
    	if ($db = false)
    	{
      $error = true;
      $error_details['number'] = 2;
      $error_details['text'] = "Cannot connect to database";
      setcookie("xsig-number", $error_details['number'], time()+60);
      setcookie("xsig-text", $error_details['text'], time()+60);
    	}
    }
    ?>
    The next file "global.php" checks to see if the user is a member or not, it includes config.php, you dont need to read this, but you can if you wish:

    Code:
    <?
    include "config.php";
    $username = $_COOKIE['xsigu'];
    $password = $_COOKIE['xsigu'];
    if ((isset($username)) && (isset($password)))
    {
    	mysql_connect($sql['host'], $sql['username'], $sql['password']);
    	mysql_select_db($sql['dbname']);
    	$users = mysql_query("SELECT * FROM xsig_users");
    	while ($row = mysql_fetch_array($users)) 
    	{
      if (($row['username'] == $username) && ($row['password'] == $password))
      {
      	$user['isuser'] = true;
      	$user['username'] = $row['username'];
      	$user['id'] = $row['id'];
      }
    	}
    	if (!isset($user['isuser']))
    	{
      $user['isuser'] = false;
      $user['username'] = "Guest";
      $user['id'] = 0;
    	}
    } else {
    	$user['isuser'] = false;
    	$user['username'] = "Guest";
    	$user['id'] = 0;
    }
    ?>
    Ok, heres my problem; logon.php if error = true takes the user to error.php, and then error.php reads the cookies using: print_r($_COOKIE);, but it displays nothing...

    Remember my fake sql details? well i check them here:

    Code:
    $link = mysql_connect($sql['host'], $sql['username'], $sql['password']);
    if ($link = false)
    {
    	$error = true;
    	$error_details['number'] = 1;
    	$error_details['text'] = "Cannot connect to host";
    	setcookie("xsig-number", $error_details['number'], time()+60);
    	setcookie("xsig-text", $error_details['text'], time()+60);
    }
    So the cookies are set right? Nope. Check it out for yourself: http://www.total-code.com/xsig/index.php Clicking on the link testing will take you to logon.php and then forward you to error.php which should display the cookies, but instread, i get this: Array ( )

  2. #2
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry to say thats what you just said is all bull-****e. But it is .

    The problem was that i forgot to use: == in an if statment, instead i only used a single =. it works now .

  3. #3
    SitePoint Evangelist jplush76's Avatar
    Join Date
    Nov 2003
    Location
    Los Angeles, CA
    Posts
    460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a good tip is to reverse your if statement parameters so you never do that

    so instead of
    PHP Code:
    if($a == 5
    you would do
    PHP Code:
    if(== $a
    this way if you forget the == php will throw an error right away because you can't assign anything to the number 5, it helps from making those errors
    My-Bic - Easiest AJAX/PHP Framework Around
    Now Debug PHP scripts with Firebug!

  4. #4
    &lt;!-- Insert thoughts here --&gt; pitcher17's Avatar
    Join Date
    Apr 2004
    Location
    The great white north
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jplush76
    a good tip is to reverse your if statement parameters so you never do that

    so instead of
    PHP Code:
    if($a == 5
    you would do
    PHP Code:
    if(== $a
    this way if you forget the == php will throw an error right away because you can't assign anything to the number 5, it helps from making those errors

    That is a great suggestion, jplush. I will be passing that one on to my students.
    The more time I save by not planning and documenting,
    the more time I have left to debug.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •