Jump to content

[SOLVED] "Keep me logged in" Checkbox feature


Irresistable

Recommended Posts

I have made a checkbox, to keep users logged in for a certain amount of time.

However..

I put if checkbox == check { set cookie ID Username; set cookie ID Password }

 

Also, I checked for the cookie further up, and if cookie is there, it logs in.

 

In my case.. I'm not sure if it sets the cookie, or its not reading the cookie, or what?

 

Here is the PHP code for the login form.

<?php session_start();   
if($_SESSION['s_logged_n'] == 'true'){
header("location: index.php");
}

include 'config.php';

//Checks if there is a login cookie
if(isset($_COOKIE['ID_username']))
//if there is, it logs you in and directes you to the members page
{ 
$username = $_COOKIE['ID_username']; 
$pass = $_COOKIE['Key_password'];
$check = mysql_query("SELECT * FROM Users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check )) 
{
if ($pass != $info['password']) 
{
	}
	else
	{
		header("Location: member.php?$username");
	}
}
}

if(isset($_POST['login']))
{
$username = trim(addslashes($_POST['username']));
$password = md5(trim($_POST['password']));

$query = mysql_query("SELECT * FROM Users WHERE Username = '$username' AND Password = '$password' LIMIT 1") or die(mysql_error());

$row = mysql_fetch_array($query);

// now we check if they are activated

if(mysql_num_rows($query) > 0)
{  if($row['Activated'] > 0)
	{

		$_SESSION['s_logged_n'] = 'true';
		$_SESSION['s_username'] = $username;
		$_SESSION['s_name'] = $row['Name'];

		if(isset($_POST['keep']) == checked)
		{
			$time = time() + 60*60*24*1000;
			setcookie(ID_username, $username, $time); 
			setcookie(Key_password, $password, $time);
		}

		header("Location: member.php?$username");

	}  
else{
$error['notactivated']='
		<p>Sorry, you must activate your account first. Please check your email for the email.</p>
		';	};
        
       }else
                  {$error['problemwithlogin']='	
	<p>There was an error processing your login, it appears that your username and/or password was incorrect. Please try again.</p>
	  ';	};

};

?>

 

Also for a quick note, once this gets working, how would I use it to set it as logged in. EG: If I came back to the website 2 days later, and instead of going to login.php, i go to the homepage, how will I set it so that it recognises me as logged in there too.

 

Whats wrong with the code? thanks.

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.