how i standardly do logins is like this:
<?php
if(ISSET($_POST['login'])){
$txtusername = $_POST['txtusername'];
$txtusername = str_replace("'",'',$txtusername);
$txtusername = str_replace('"','',$txtusername);
$txtpassword = $_POST['txtpassword'];
$txtpassword = str_replace("'",'',$txtpassword);
$txtpassword = str_replace('"','',$txtpassword);
$sql = "SELECT `user_id` FROM cookie_tbl where name = '$txtusername' and
password = '$txtpassword'";
$result= mysql_query($sql);
if($result !== false && mysql_num_rows($result)>0){
$userdata = mysql_fetch_assoc($result);
$token = md5(rand());
//should check if token already exists in table and make a new one if it does
//otherwise a user could login as someone else if the token happens to match(unlikely)
$tokenQuery = "UPDATE `cookie_tbl` SET `token` = '{$token}' WHERE `user_id` = {$userdata['user_id']}";
if(mysql_query($tokenQuery)){
setcookie('token',$token,time()+(60*60*8),'/');
header('location:userlogin.php');
}else{
echo "error setting token";
}
}else{
echo "invalid username and password";
}
}
?>
The on each page of the site i add a required include like so:
-> index.php
<?php
require_once 'app.php';
if($appData['login']){
//logged in
echo "Welcome back <span style='color:{$appData['some_preference']};'>{$appData['user_name']}</span>";
}else{
header('Location: userlogin.php');
}
?>
then the included file checks if the user is logged in or not and pulls some data you might want to personalise the site:
-> app.php
<?php
$appData = array();
if(ISSET($_COOKIE['token']) && ctype_alnum($_COOKIE['token'])){
$checkTokenQuery = "SELECT `user_id`,`user_name`,`some_preference` FROM `cookie_tbl` WHERE `token` = '{$_COOKIE['token']}";
$chkResult = mysql_query($checkTokenQuery);
if($chkResult !== false && mysql_num_rows($chkResult) == 1){
$appData = mysql_fetch_assoc($chkResult);
$appData['login'] = true;
}else{
$appData['login'] = false;
}
}else{
$appData['login'] = false;
}
?>
Sessions are basically temporary data stored on your hosting server for a client connecting to you, it generates a random token, much like the cookie setup above,to validate your session then once you validate the server has access to variables set whilst that user is connected. such as setting $_SESSION['name'] = 'Biiim'; on one page, once i open another page the session token gets passed along and it will remember that var has been set so you can re-use it by doing echo $_SESSION['name'];.
Cookies are data stored on the users computer so say you set $_COOKIE['name'] = 'Biiim'; that data is stored on my browser and i can go in and edit it, it also requires no validation cause its on my pc anyone could create that cookie without your site even creating it, the cookie method above uses a cookie called token which is some large random string, very hard to guess, the script uses that string to validate a user has logged in correctly and has to match the exact token your script created for the user.
I just set a token cookie then store all other data within mysql, that way it doesnt get lost.
Effectively using that method there is little difference but sessions will always be more secure since the data is stored on your server a person could log on to the users computer and browse his stored cookies - not a good idea to store passwords in cookies. If you want to be really secure you need to use ssl(https) which encrypts data requests so your token can't get hijacked(the thing that identifys you), you generally dont need that unless you are transmitting card details or something quite personal/valuble though.