Jump to content

setcookie not working


doddsey_65

Recommended Posts

when a user logs in the cookie isnt being set. am i doing it wrong?

 

if(empty($error))
    {
        $query = $link->query("SELECT *
                        FROM ".TBL_PREFIX."users
                        WHERE u_username = '$username'
                        AND u_password = '".asf_hash($password)."'")
                        or die(print_link_error());
        $row = $query->fetchAll();
        $num_rows = $query->rowCount();
        
        if($num_rows == 1)
        {
            if($row[0]['u_confirmed'] == 1)
            {
                setcookie('uid', $row[0]['u_uid'], time() + $session_length); // this cookie isnt being set
                echo 1;
            }
            else
            {
                $error = 'You Need To Activate Your Account';
            }
        }
        else
        {
            if(!$error)
            {
                echo $lang->incorrect_login_details;
            }
        }
    }

 

im doing a print_r on all cookies and it doesnt appear in the list.

$session_length is set at 99999999

Link to comment
Share on other sites

PHP manual: "setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.

When the cookie is not set, a FALSE is returned."

So I suggest that you check the result of setcookie() to check, as in:

if (setcookie('uid', $row[0]['u_uid'], time() + $session_length) === false) {// this cookie isnt being set
    echo 'Setcookie failed';
    exit;
}

Link to comment
Share on other sites

i have tried:

 

if (setcookie('uid', $row[0]['u_uid'], time() + $session_length) == false) 
                {
                    echo 'Setcookie failed';
                }
                else
                {
                    echo 'success';
                }

 

and it echos success, but no cookie is added. Could this be something to do with WAMP server?

Link to comment
Share on other sites

when the processing of the login is complete it passes a value to a javascript file which then redirects to the page they were last on, or the index page if no redirect session exists. Im trying to upload the files to the server to see if this is a localhost thing but filezilla always breaks down so its taking a while lol. also does anyone know why i might have phpbb3 cookies on my localhost site? i do have a setup of phpbb in another folder but not this one.

Link to comment
Share on other sites

ok here is the whole file, i hope someone can help. when it is successful it echos 1 back to the javascript file which redirects to the page they were on or redirects back to index.php if there isnt a redirect session.

 

<?php ob_start();
/* ASF A Simple Forum
* Version 0.1.0
* Page Created 21/01/2011
*/

// can page be accessed directly?
define('ACCESS', TRUE);

// start sessions
session_start();

// get the functions file
include(dirname(dirname(__FILE__)).'/functions/functions.php');
include(dirname(dirname(__FILE__)).'/includes/initialize.php');

if(isset($_POST['user_name']) && isset($_POST['password']))
{
    $username = $_POST['user_name'];
    $password = $_POST['password'];
    $session_length = $_POST['session_length'];
    
    if(empty($session_length) &&!$error)
    {
        $error = 'Invalid Session Length';
    }
    
    if(empty($username))
    {
        if(!$error)
        {
            $error = 'Please Enter A Username';
        }    
    }
    
    if(empty($password))
    {
        if(!$error)
        {
            $error = 'Please Enter A Password';
        }
    }
    
    if(empty($error))
    {
        $query = $link->query("SELECT *
                        FROM ".TBL_PREFIX."users
                        WHERE u_username = '$username'
                        AND u_password = '".asf_hash($password)."'
                        LIMIT 1")
                        or die(print_link_error());
        $row = $query->fetchAll();
        $num_rows = $query->rowCount();
        
        if($num_rows == 1)
        {
            if($row[0]['u_confirmed'] == 1 && $row[0]['u_banned'] == 0)
            {
                setcookie("uid", "1", time()+3600);
                
                if (setcookie('uid', "1", time()+3600) === false) 
                {
                    echo 'Failed To Set Cookie';
                }
                else
                {
                    echo 1;
                }    
            }
            elseif($row[0]['u_banned'] == 1)
            {
                $error = 'You Have Been Banned From This Forum';
            }
            else
            {
                $error = 'You Need To Activate Your Account';
            }
        }
        else
        {
            if(!$error)
            {
                echo $lang->incorrect_login_details;
            }
        }
    }
    else
    {
        echo $error;
    }
}
ob_flush();
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.