The_Black_Knight Posted December 1, 2007 Share Posted December 1, 2007 Hi I am having trouble combining my login script with session and cookie. As I understand session data is only saved in the running browser, and when the browser is closed the data will be deleted. While cookies will store the data for a set time until the cookie terminates or it is removed by the php script (logout), browser. The data from the cookie is accessible the next time the browser is used. So I could really need some examples of how it could be done <?php //Admin Login include '../include/language.php'; include '../include/style.php'; print "<html>"; print "<body>"; print "<head>"; print "<title>".$pagetitel." - Admin Login</title>"; print "</head>"; print "<link rel=\"icon\" href=\"../../images/favicon".$fileextension."\" type=\"image/x-icon\">"; print "<link rel=\"shortcut icon\"href=\"../../images/favicon".$fileextension."\" type=\"image/x-icon\">"; print "<link rel=\"stylesheet\" type=\"text/css\" href=\"../include/style.css\">"; print "<body bgcolor=".$browserbg.">"; print "<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" width=\"750\" align=\"center\" bordercolor=".$bordercolor." bgcolor=".$framebg.">"; print "<tr>"; print "<td align=\"center\"><img border=\"0\" src=\"./../../images/logo".$logofileextension."\" align=\"center\" width=\"720\">"; print "<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" width=\"720\" align=\"center\" bordercolor=".$bordercolor.">"; //print "<tr><td bgcolor=".$browserbg." align=\"center\" colspan=\"3\"><font color=".$fontcolor." face=".$font." size=\"3\"><b>".$pagetitel." - Admin Login</b></font></td></tr>"; print "</table>"; print "<br><br><br>"; //Login Form print "<center>"; print "<table border=\"0\" cellspacing=\"0\">"; print "<tr>"; print "<form action=\"../admin/admin.php\" method=\"post\">"; print "<tr>"; print "<td class=\"tablehead\" bgcolor=".$tablehead."><div align=\"center\" class=\"Stil1\">Admin Login</div>"; print "</td>"; print "<tr>"; print "<tr>"; print "<td bgcolor=".$tablebg."><div align=\"left\" class=\"Stil1\">Username:</div>"; print "<input type=\"text\" name=\"username\">"; print "</td>"; print "</tr>"; print "<tr>"; print "<td bgcolor=".$tablebg."><div align=\"left\" class=\"Stil1\">Password:</div>"; print "<input type=\"password\" name=\"password\">"; print "</td>"; print "<tr>"; print "<td bgcolor=".$tablebg."><div align=\"left\" class=\"Stil1\">Remember Me: <input type=\"checkbox\" name=\"rememberme\"></div>"; print "</td>"; print "</tr>"; print "<tr>"; print "<td bgcolor=".$tablebg." align=\"center\">"; print "<input type=\"submit\" value=\"Login\">"; print "</td>"; print "</tr>"; print "</form>"; print "</table>"; print "</center>"; print "<br><br><br>"; print "</body>"; print "</html>"; ?> <?php //Admin Area error_reporting(E_ALL & ~E_NOTICE); include '../include/dbconnect.php'; include '../include/language.php'; include '../include/style.php'; //User & Password check against DB $username = mysql_real_escape_string($_POST['username']); $password = mysql_real_escape_string($_POST['password']); $rememberme = mysql_real_escape_string($_POST['rememberme']); $query = "SELECT * FROM ".$dbprefix."users WHERE username='$username'"; //AND password='$password' [color=red]<----- I also having some trouble with making a combined check on username + password[/color] $result = mysql_query($query); $data = mysql_fetch_object($result); /* Some pieces of code I have found on the net: //Check if there is a cookie file //Check if there is a session file if (isset($_COOKIE['$cookie'])){ $username = $_SESSION'username'; $password = $_SESSION'password'; } //Check if checkbox "Remember Me" checked then save cookie if (isset($rememberme)){ setcookie(ID_"$cookie", $username, time() + 3600, "/admin/", "$cookie", 1); setcookie(Key_"$cookie", sha1($password), time() + 3600, "/admin/", "$cookie", 1); } ---------LOGOUT------------------ if(isset($_POST[logout])){ session_destroy(); setcookie ("user", "$username", time() - 3600, "/admin/", "$cookie", 1); header('Location: ../'); die; } ------------SESSION--------------- session_start(); if (isset($_SESSION'username') && isset($_SESSION'password')){ $username = $_SESSION'username'; $password = $_SESSION'password'; } ---------------------------------- */ //Restricted Area - Access Granted if(($data->username == $username) && ($data->password == sha1($password))){ HERE GOES THE PROTECTED SCRIPT } //Restricted Area - Access Denied else{ print "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\" width=\"720\" bordercolor=\"000000\">"; print "<td align=\"center\" colspan=\"2\" bgcolor=\"\"><font color=\"000000\" face=\"arial\" size=\"2\">"; print "<b>This is a Restricted Area. You do not have access!</b>"; print "<br>"; print "<br>"; print "<b>You will now be send back to the Mainpage!</b>"; print "<meta http-equiv=\"Refresh\" content=\"10; url=../../index.php\">"; print "</font></td>"; print "</table>"; } ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted December 1, 2007 Share Posted December 1, 2007 Search on Sessions and Cookies and you'll find quite a few examples. So I could really need some examples of how it could be done Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.