<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name="user"; // Table name


mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$time = time();
$check = $_POST['checkbox']; 


$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE user_id='$myusername' and password='$mypassword'";
$result=mysql_query($sql);


$count=mysql_num_rows($result);





if($count==1)
{
	if($check)
	 {
   
    setcookie('myusername', $username, $time + 3600);        // Sets the cookie username
    setcookie('mypassword', $password, $time + 3600);    // Sets the cookie password
 	}
   session_register("myusername");
   session_register("mypassword");

	header("location:login_success.php");
}
else {
echo "Wrong Username or Password / You don't fill the box correctly ";
}
?>

I'm new in PHP, now i'm trying to make a menu log in with remember me..there is no error in code above, but the password and username can't be remember...

Is there someone can help me?please i need ur help..thx..

the basic principle is to set a cookie http://php.net/manual/en/function.setcookie.php and get the cookie

setcookie("username", $username);

and then get the cookie

$username = $_COOKIE['username'];

however that's not secure.


so you should hash the name, maybe salt it, and set a second cookie to verify the first. Otherwise, people could log in as other users.

try this .......

<?php
if(($_COOKIE['myusername'] && $_COOKIE['mypassword']))
{
	echo "already login";
}
 if(isset($_POST['login']))
{
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="abc123"; // Mysql password
$db_name="ps"; // Database name
$tbl_name="user"; // Table name
 
 
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
 
 
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$time = time();
$check = $_POST['checkbox'];  

 
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
 
$sql="SELECT * FROM $tbl_name WHERE UserName='$myusername' and Password='$mypassword'";
$result=mysql_query($sql);
 
 
$count=mysql_num_rows($result);

	if($count==1)
	{
		
		session_start();
		$_SESSION['myusername']=$myusername;
		$_SESSION['mypassword']=$mypassword;
		if(isset($_POST['checkbox']))
		 {
			
	   
			setcookie('myusername', $username, $time +60*60*24*100, "/");        // Sets the cookie username
			setcookie('mypassword', $password, $time +60*60*24*100, "/");    // Sets the cookie password
		 }
	  
	   
		header("location:login_success.php");
		
	   }
}
else
{
	echo "PLease fill all";}
?>

note: the above solution is not secure and very hackable for the reasons that I previously stated

i know that i reply according to his question......

if(isset($check)){
setcookie("myuserid", $_SESSION['myuserid'], time()+60*60*10, "/path/to/website/");
setcookie("myusername", $_SESSION['myusername'], time()+60*60*10, "/path/to/website/");
}

Then on your protected pages put this after the 'session_start()'

//check for cookies

if(isset($_COOKIE['myuserid']) && isset($_COOKIE['myusername'])){
      $_SESSION['myuserid'] = $_COOKIE['myuserid'];
      $_SESSION['myusername'] = $_COOKIE['myusername'];
   }


if (!isset($_SESSION['myuserid']))
{
header("Location: login.php");
}

This will set the cookie for 10hours. Also sending the password to a cookies is a big security risk.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.