Jump to content

Problems with functions and cookies


pedro84

Recommended Posts

Hi there,

 

I got login script. I wanted to add  some feature to remember user's login for some time.

 

But first, some code. Function auto_login_cookie sends cookie if user want to, function auto_login checks the cookies. In the cookies only hashed username and user's uniqid are stored.

 

function auto_login_cookie (){
if (isset($_POST['staylogged']) && $_POST['staylogged'] == 1)
    $username = $_SESSION['username']; 
    $query = mysql_query("SELECT uniqid, username FROM users WHERE username = '$username'  limit 1") or die ('Error');
    $r = mysql_fetch_assoc($query);

setcookie("bbuniqueidhash", $r['uniqid'], time() + 30 * 86400);
setcookie("bbusernamehash", md5($r['username']), time() + 30 * 86400, "/", ".traderslists.com", 1);
}
function auto_login (){
    $username = $_SESSION['username']; 
    $query = mysql_query("SELECT uniqid, username FROM users WHERE username = '$username'  limit 1") or die ('Error');
    $r = mysql_fetch_assoc($query);
if (isset($_COOKIE['bbuniqueidhash']) && $_COOKIE['bbuniqueidhash'] == $r['uniqid'] AND (isset($_COOKIE['bbusernamehash']) && $_COOKIE['bbusernamehash'] == md5($r['username']))){
return true;
} else {
return false;
}
}

 

Ok. This functions checks if user is logged:

function is_authed()
{
     if (isset($_SESSION['username']) && (md5($_SESSION['username']) == $_SESSION['encrypted_name']))
     {
          return true;
     }
     else
     {
          return false;
     }
}

and I check it this way:

	if (!is_authed())
{
     die ('You are not permitted to view this page, <a href="login.php">click here</a> to login.');
}

 

 

There're some problems with that script.

 

First of all, when I'm comparing cookies with the data in database everything is ok. Cookies are sending correctly if I want to, and data are equal with the data in database. But...how to call that function? I tried:

auto_login();
if (!is_authed())
{
     die ('You are not permitted to view this page, <a href="login.php">click here</a> to login.');
}

but it does not work. I tried to make it many ways withou any positive result. Have you got any ideas and suggestions?

 

Second case, I tested everything on my local server, but when I tried to debug on the webserver, cookies are not sending:( What did I do wrong?

 

Need help:)

 

Later,

Pedro

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.