SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Cookie not being set.

    Hi all i have this code which is getting setting the username and password if it matches and doesnt seem to be working correctly with my login this i what i have atm.

    PHP Code:
    error_reporting(E_ALL);
        require(
    "dbconnect.php");
                
    $msg_pass="";
                
    $msg_user="";
                
    $username mysqli_real_escape_string($con,stripslashes(trim($_POST['username'])));
                    
    $password mysqli_real_escape_string($con,stripslashes(trim($_POST['password'])));
                    
    $cQuery="SELECT * FROM members WHERE username='".$username."' AND Password='".$password."'";
                    
    $con;
                    
    //echo  $cQuery;
                    
    $rs=mysqli_query($con,$cQuery);
                    if(!
    $rs)
                    {
                        echo 
    "Unable to excute the query:".mysqli_error($con);
                    }
                    else
                    {
                        
    $count=mysqli_num_rows($rs);
                        if(
    $count>0)
                        {
                            
    $data=mysqli_fetch_assoc($rs);
                            if(
    $data['password']=$password)
                            {
                                if(
    $remember)
                                {
                                    echo 
    "Remember is being set";
                                    
    //
                                    
                                    
    $time time();
                                    
    setcookie("BCCMS",$username,$time +3600);
                                    
    setcookie("BCCMS",$password,$time +3600);
                                    
    $today=date("Ymd");
                                    
                                    if(isset(
    $_COOKIE['BCCMS'])) 
                                    {
                                        echo 
    "Cookie has been set cookie monster is happy";
                                        
    $_SESSION['user']=$username;
                                        
    //
                                        
    $dQuery="UPDATE members SET LastedLogin='$today',signons=signons+1,onlinestatus='1' WHERE username='".$COOKIE["BCCMS"]."'";
                                        echo 
    $dQuery
                                        
    $rs=mysqli_query($con,$dQuery);
                                        if(!
    $rs)
                                        {
                                            echo 
    "Error:".mysqli_error($con);
                                        }
                                        else
                                        {
                                            echo 
    "";
                                        }
                                        
    //
                                    
    }
                                    else
                                    {
                                        echo 
    "Cookie monster is unhappy";
                                    }
                                    echo 
    "Logged in as:&nbsp;".$_SESSION['user']."<br/>";
                                    echo 
    "Please Wait while we log you in";
                                    
    //echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=members.php\">";
                                    //    
                                
    }
                                else
                                {
                                    echo 
    "remember me feature not being used";
                                    
    $_SESSION['user']=$username;
                                    
    //
                                    
    $today=date("Ymd");
                                    
    $dQuery="UPDATE members SET LastedLogin='$today',signons=signons+1,onlinestatus='1' WHERE username='".$_SESSION['user']."'";
                                    
    $rs=mysqli_query($con,$dQuery);
                                    if(!
    $rs)
                                    {
                                        echo 
    "Error:".mysqli_error($con);
                                    }
                                    else
                                    {
                                        echo 
    "";
                                    }
                                    echo 
    "Logged in as:&nbsp;".$_SESSION['user']."<br/>";
                                    echo 
    "Please Wait while we log you in";
                                    
    //echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=members.php\">";
                                    //
                                
    }
                                
                            }
                            else
                            {
                                
    $msg_pass="Wrong Password,Please Try again<br>\n";
                                
    $rmt $_SERVER["REMOTE_ADDR"];
                                
    $today=date("F j,Y,g:i a");
                                if(
    $rmt || $today)
                                {
                                    
    $pQuery="insert into failedlogins values('','$username','$password','$rmt','$today')";

                                    
    $result=mysqli_query($con,$pQuery);
                                    if(!
    $result)
                                    {
                                        echo 
    "Error:".mysqli_connect_errno();
                                        echo 
    "<br/>Error2:".mysqli_error($con);
                                    }
                                    else
                                    {
                                        echo 
    "Your Ip Address has been Logged!";
                                    }
                                }
                            }
                        }
                        else
                        {
                            
    $msg_user="Wrong Username,Please Try again<br>\n";
                            
    $rmt $_SERVER["REMOTE_ADDR"];
                            
    $today=date("F j,Y,g:i a");
                            if(
    $rmt || $today)
                            {
                                
    $pQuery="insert into failedlogins values('','$username','$password','$rmt','$today')";

                                
    $result=mysqli_query($con,$pQuery);
                                if(!
    $result)
                                {
                                    echo 
    "Error:".mysqli_connect_errno();
                                    echo 
    "<br/>Error2:".mysqli_error($con);
                                }
                                else
                                {
                                    echo 
    "Your Ip Address has been Logged!";
                                }
                            }
                        }
                    } 
    When i do the check if remember me is checked it goes into it but when i do i

    PHP Code:
    if(isset($_COOKIE['BCCMS'])) 
                                    {
                                        echo 
    "Cookie has been set cookie monster is happy";
                                        
    $_SESSION['user']=$username;
                                        
    //
                                        
    $dQuery="UPDATE members SET LastedLogin='$today',signons=signons+1,onlinestatus='1' WHERE username='".$COOKIE["BCCMS"]."'";
                                        echo 
    $dQuery
                                        
    $rs=mysqli_query($con,$dQuery);
                                        if(!
    $rs)
                                        {
                                            echo 
    "Error:".mysqli_error($con);
                                        }
                                        else
                                        {
                                            echo 
    "";
                                        }
                                        
    //
                                    
    }
                                    else
                                    {
                                        echo 
    "Cookie monster is unhappy";
                                    } 
    Just prints out saying cookie monster is unhappy for cookie not being set when it should be not sure what i am doing is wrong?

    Can anyone help me?

    Thanks,William

  2. #2
    SitePoint Zealot Gar onn's Avatar
    Join Date
    Feb 2011
    Location
    Belgium
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see:
    PHP Code:
                                    echo "Remember is being set"
                                    
    // 
                                     
                                    
    $time time(); 
                                    
    setcookie("BCCMS",$username,$time +3600); 
                                    
    setcookie("BCCMS",$password,$time +3600); 
    echo folowed by setcookie, this should return an error becouse 'header is already sent ... bla bla bla'

    setcookie should be called before any output has been send

  3. #3
    SitePoint Zealot Klav's Avatar
    Join Date
    Mar 2002
    Location
    Huddersfield, UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you require content to be shown before setting the cookie, add the following to the very top of the script where the cookie is set;

    PHP Code:
    ob_start("ob_gzhandler"); 
    This turns on gzip output buffering and allows headers to be set after content has been outputted.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Cookies set are not available until the next request.
    The only code I hate more than my own is everyone else's.


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
  •