Page 1 of 1
PHP Sessions and Cookies
Posted: Sat May 28, 2011 11:14 pm
by Pazuzu156
When I use my login form, I login with sessions. If i close the browser sessions end. How to i make it where the session stays if the browser is closed with cookies? Or is this even possible?
Re: PHP Sessions and Cookies
Posted: Sun May 29, 2011 1:04 am
by Pazuzu156
Never mind, i figured it out on my own. For the future if anyone needs something like this answered here is a sample code:
connect.php:
Code: Select all
<?php
mysql_connect("localhost","root","") or die (mysql_error());
mysql_select_db("cms") or die (mysql_error());
?>
login.php
Code: Select all
<?php
// connect to database where users are stored
require 'connect.php';
// set variables for info sent over from login form
$username = $_POST['username'];
$password = $_POST['password'];
$rememberme = $_POST['rememberme'];
// check if the user entered in the username and password
// else tell them to do so if they don't
if($username&&$password) {
// hash the password
$password = md5($password);
// use sprintf to secure the login process
$login = sprintf("SELECT * FROM users WHERE username='%s' AND password='%s'", mysql_real_escape_string($username), mysql_real_escape_string($password));
// check if user checked the remember me box, if not
// then run code in else statement
if(isset($rememberme)) {
// run the query
$query = mysql_query($login);
// get number of rows
$numrows = mysql_num_rows($query);
// check if username entered matches password that goes along with username provided
// else tell them the username and password combo do not match
if($numrows==1) {
// set cookie time for a year
setcookie('login',$username,time()+60*60*24*365);
// set the session = the cookie
$_SESSION['cms_user'] = $_COOKIE['login'];
// give a friendly welcome and allow then to go home
echo 'Welcome, ' . $_SESSION['cms_user'] . '! You successfully logged in. <a href="./">Return home</a>.';
} else {
echo 'Incorrect username/password conbination';
}
} else {
$query = mysql_query($login);
$numrows = mysql_num_rows($query);
if($numrows==1) {
// set cookie time to expire when the browser is closed
setcookie('login',$username,false);
$_SESSION['cms_user'] = $_COOKIE['login'];
echo 'Welcome, ' . $_SESSION['cms_user'] . '! You successfully logged in. <a href="./">Return home</a>.';
} else {
echo 'Incorrect username/password conbination';
}
}
} else {
echo 'You need to enter in a username and password';
}
?>
index.php
Code: Select all
<?php
require 'connect.php';
session_start();
$_SESSION['user'] = $_COOKIE['login'];
?>
<html>
<head>
<title>Login Form</title>
</head>
<body>
<?php
if(isset($_SESSION['user'])) {
?>
Hello <?php echo $_SESSION['user']; ?>! You are currently logged in. <a href="./logout.php">Logout</a>
<?php
} else {
?>
<table align="center">
<th>
Login Form
</th>
<tr>
<td>
<form method="post" action="login.php">
<input type="text" name="username" onclick="if(this.value=='Username')this.value='';" onblur="if(this.value=='')this.value='Username';" value="Username"><br>
<input type="password" name="password" onclick="if(this.value=='Password')this.value='';" onblur="if(this.value=='')this.value='Password';" value="Password"><br>
<input type="submit" name="login" value="Login"> <input type="checkbox" name="rememberme">Remember Me
</form>
</td>
</tr>
</table>
<?php } ?>
</body>
</html>
logout.php
Code: Select all
<?php
// begin the session, then destroy it to lean the session
session_start();
session_destroy();
// set cookie to expire when the browser is closed
// and remove anything the cookie may hold
setcookie("login",'',time()-3600);
unset($_COOKIE['login']);
// tell the user the logout has been successfull
echo 'You have successfully logged out. Return to the <a href="./index.php">main page</a>.';
?>