www.webdeveloper.com
Results 1 to 4 of 4

Thread: cookie name issues

  1. #1
    Join Date
    Oct 2011
    Location
    Hamilton, Ontario
    Posts
    74

    cookie name issues

    Okay so I'm running this script to set a cookie with the same name as the username of the person who logs in, but when I check my cookies after I run the script, the only cookie that is set from the host I am working with is named 'PHPSESSID' instead of the username entered. What am I doing wrong?
    PHP Code:
    require('db.php');
    if(
    $_SERVER["REQUEST_METHOD"] == "POST"){
        
    $returned=$db->query("SELECT * FROM basic WHERE username='".$_POST["username"]."' and pass='".$_POST["password"]."'");
        
    $returned->execute();
        
    $row=$returned->fetchAll(PDO::FETCH_ASSOC);
        if(
    count($row)==1){
            
    $results=$row[0];
            
    $username=$results['username'];
            
    setcookie($username,'mp'.$username,60,'/','localhost',false,true);
            
    session_start();
            
    header('location: profile.php?username='.$username);
        }else{
    $message='no match found';}


  2. #2
    Join Date
    Sep 2008
    Location
    Akron, OH
    Posts
    1,105
    Maybe the session needs to start before you set the cookie?
    I'm always up for networking with fellow web professionals. Connect with me on LinkedIn if you like!

  3. #3
    Join Date
    Oct 2011
    Location
    Hamilton, Ontario
    Posts
    74
    tried that... it didn't work

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,926
    Turn on all error-reporting (or check the PHP error log) and make sure you're not trying to set the cookie after headers have already been sent.

    Also, is it really your intention for each cookie's name to be the value of $username? Not that you can't and it might even make sense is some special case; but most typically the first parameter to setcookie() would be a string literal, the the second parameter would have the value of interest. Also, I don't think you want to use 'localhost' as the domain -- either let it be the default, or else use the domain the user would be using to request the page.
    PHP Code:
    setcookie('user_name'$username0'/''.yourdomain.com'); // leading dot for domain = all sub-domains 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles