Set / Unset / Load Cookies

Written by @m_k_amin 20 November 2012

Cookie is a common way to store data on users PC. Such data used, for example, to know whether the user is logged in before or not. In this script we have made very simple login system to show how the cookies are used. You can find how to set, load or delete a cookie in this code. Enjoy!

Code Snippet:

                                                
                                                <!-- this script is provided by https://www.phpfreecode.com coded by: Kerixa Inc. -->
<!-- This Script is from www.phpfreecpde.com, Coded by: Kerixa Inc-->

<?php
function welcome($msg){
$username='Admin';
die('
<table style="border-width: 0px;width: 400px; height: 107px">
	<tr>
		<td style="border-style: solid;border-width: 0px;font-size: 17pt;background-color: #DFDFFF;">'.$msg.'</td></tr><tr>
		<td style="border-style: solid;border-width: 0px;font-size: 17pt;background-color: #DFDFFF;"><strong>Welcome '.$username.'</strong><br>
		<a href="'.$_SERVER['PHP_SELF'].'?type=logout"><span style="border-style: solid;border-width: 0px;background-color: #DFDFFF;">Logout</span></a></td>
	</tr>
</table>
');
}
$login='no';
if (isset($_COOKIE['username'])) $login='yes';

if (isset($_GET['type']) && $_GET['type']=='login'){
	if ($_GET['username']='Admin' && $_GET['password']='12345')
		setcookie('login','true',time()+3600);
		$login='yes';
}elseif (isset($_GET['type']) && $_GET['type']=='logout'){
		setcookie('login','',time()-3600);
		$login='no';
}

if ( $login=='yes')	
	welcome ('You are successfully logged in!');
else{
?>

<table style="border-width: 0px;width: 212px; height: 179px" align="center" cellpadding="0" cellspacing="0">
<tr>
		<td style="border-style: solid;border-width: 0px;background-color: #DFDFFF;">
		<form action="<?php echo $_SERVER['PHP_SELF'].'?type=login'?>" method="post" ><h1>Login</h1>
<table style='border:0px solid #000000;'>
<tr>
<td align='right'>
Username: 
<input type='text' size='15' maxlength='25' name='username' value="Admin">
</td>
</tr>
<tr>
<td align='right'>
Password: 
<input type='password' size='15' maxlength='25' name='password' value="12345">
</td>
</tr>
<tr>
<td align='center'>
<input type="submit" value="Login">
</td>
</tr>
</table>
		</form>
		</td>
	</tr>
</table>

<?php
}
?>

<br><font face="Tahoma"><a target="_blank" href="http://www.phpfreecode.com/"><span style="font-size: 8pt; text-decoration: none">PHP Free Code</span></a></font><a target='_blank' href='https://www.phpfreecode.com' style='font-size: 8pt; text-decoration: none'>Php Best Codes</a>                                                
                                            

Example:


About @m_k_amin

This user is pending a biography.

M

Comments


Here you can leave us commments. Let us know what you think about this code tutorial!

0 / 300

TRENDING POST
1
2
3
4
5
VISITORS
Online Users: 12
Recent Members: grkkid, Manaakividuinfo.com, karticksv, sava, tinatina
advertisement 2