/    Sign up×
Community /Pin to ProfileBookmark

Cookies and Sessions

I’m trying to make a login script that if the user checks the remember me option there information will be stored in a cookie if not then the information is stored in sessions i dont know that about about cookies in php i know the function for it just not how to use it.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@Jarrod1937Nov 06.2010 — If the information being saved is possibly sensitive, then i'd recommend always using sessions to store their info, and instead have the "remember me" option simply change how long the cookie and session last (set no expiration time and the cookie is destroyed at the end of the session).

The cookies are automatically handled by php if you use their built in session system. However, if you want to manually set a cookie and its contents use setcookie(); (site is complete with explanation and examples).
Copy linkTweet thisAlerts:
@BelrickNov 07.2010 — Here is my simple script which uses cookies.

I cannot for the life of me figure how you would store login in details using sessions that of course expire upon close.

Perhaps I have my wires crossed?

Anywho heres client side login in form with remember me option

[code=html]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Login Form</title>
<link href="loginmodule.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
function delete_cookie (name, path)
{
if (name != "login")
{
// Build expiration date string:
var expiration_date = new Date ();
expiration_date . setYear (expiration_date . getYear () - 1);
expiration_date = expiration_date . toGMTString ();

// Build set-cookie string:
var cookie_string = escape (name) + "=; expires=" + expiration_date;
if (path != null)
cookie_string += "; path=" + path;

// Delete the cookie:
document . cookie = cookie_string;
}
}

function delete_all_cookies (path)
{
// Get cookie string and separate into individual cookie phrases:
var cookie_string = "" + document . cookie;
var cookie_array = cookie_string . split ("; ");

// Try to delete each cookie:
for (var i = 0; i < cookie_array . length; ++ i)
{
var single_cookie = cookie_array [i] . split ("=");
if (single_cookie . length != 2)
continue;
var name = unescape (single_cookie [0]);
delete_cookie (name, path);
}
}


function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1)
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}

function setCookie(c_name,value,expiredays)
{var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function rememberlogin()
{
login = document.getElementById('login').value;
password = document.getElementById('password').value;
rememberme = document.getElementById('rememberme').checked;

if (rememberme)
{
setCookie('remembermelogin',login,365);
setCookie('remembermepassword',password,365);
setCookie('rememberme',rememberme,365);
}
else
{
delete_all_cookies ();
setCookie('rememberme',rememberme,365);
}

}

function rememberedlogin()
{

rememberme = getCookie('rememberme');

if (rememberme == 'true')
{
login = getCookie('remembermelogin');
password = getCookie('remembermepassword');


document.getElementById('login').value = login;
document.getElementById('password').value = password;
document.getElementById('rememberme').checked=true;
}

}


</script>


</head>
<body onload="rememberedlogin();">

<p>&nbsp;</p>
<form id="loginForm" name="loginForm" method="post" action="login-exec.php">
<table width="380" border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<td colspan="2"><?php echo $message; ?></td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td width="132"><b>Login</b></td>
<td width="268"><input name="login" type="text" class="textfield" id="login" size='36'/></td>
</tr>
<tr>
<td>(email address)</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><b>Password</b></td>
<td><input name="password" type="password" class="textfield" id="password" size='36'/></td>
</tr>
<tr>
<td></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<input type="submit" name="Submit" value="Login" onclick="rememberlogin();"/>
<span>Remember Me<input type="checkbox" name="rememberme" value="Yes" id="rememberme" /></span>
</td>
</tr>
</table>
</form>
</body>
</html>


[/code]
Copy linkTweet thisAlerts:
@BelrickNov 07.2010 — Also you may be interested in this little tip.

In the login exec.php you can have a bunch of reasons as to why the login will fail.

eg:

[code=php]
$qry="SELECT * FROM users WHERE email = '$login' AND password='$password'";
$result=mysql_query($qry);
//Check whether the query was successful or not
if($result) {
//all good
}
else{ //no good
header("location: login-form.php?reason=ne");
}

[/code]


So within the login form script above you can add this in the body

[code=php]
<?php

@$reason=$_GET["reason"];
$message = "&nbsp;";

switch ($reason){
case "ne":
$message = "Sorry but we are having server issues, please try again later";
break;
//Have as many failed to login errors you like from incorrect login or login already in use etc
}
?>

echo $message; //Add this anywhere in the form page you like

[/code]


And now upon login failure you can display reasons why without a seperate login-failed.php page
×

Success!

Help @Yokiest spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...