Hello,
I recently posted a new thread about a php cookie, something on the lines of that. During the time it was up i fiddled around with my coding and have now got this.
I will show part of the four files below but only the cookie part (if you need more then just ask) -
- login file
-process file
-/user_area file
-log out file
Login file
<?php
require_once("db/connect.php");
include("process.php");
//check if logged in
if (loggedin())
{
header("Location: user_area/");
exit();
}
//Field Data
else if (isset($_POST['Submit'])){
$username = (isset($_POST['username'])) ? $_POST['username'] : '';
$password = (isset($_POST['password'])) ? $_POST['password'] : '';
$rememberme = $_POST['rememberme'];
$submitted = $_POST['Submit'];
if ($rowAccount){
if ($rememberme=="on")
setcookie("id", $rowAccount['username'], time()+7200);
else if ($rememberme=="")
$_SESSION['id'] = $rowAccount['username'];
header("Location: user_area/");
exit;
}else{
$error['checklogin'] = "Wrong username or password";
}
}
}
?>
<div id="login">
<form id="form1" name="form1" method="post" action="index.php">
<div id="field"><label id="login-label">Username</label><br /><input type="text" id="input" name="username" size="34" value="<?php echo (isset($_POST['username'])) ? $_POST['username'] : ''; ?>" /></div>
<div id="field" style="margin-top:20px;"><label id="login-label">Password</label><br /><input type="password" id="input" name="password" size="34" /></div>
<div id="field"><label id="login-label">Remember Me: </label><input type="checkbox" name="rememberme" value="1"></div>
<input type="hidden" id="submitted" name="submitted" />
<input type="submit" id="Submit" name="Submit" />
</form>
</div>
</div>
</div>
</body>
</html>
Process file
<?php
include("db/connect.php");
session_start();
//Check if user is logged in
function loggedin()
{
if(isset($_SESSION['id']) || isset($_COOKIE['id']))
{
$loggedin = TRUE;
return $loggedin;
}
}
?>
User_area file
<?php
require_once("../db/connect.php");
include("../process.php");
//check if logged in
if (!loggedin())
{
header("Location: ../");
exit();
}
include("../include/security.php");
And lastly my logout file
<?php
session_start();
session_destroy();
//unset cookie
setcookie("id","",time()-7200);
header("Location: ../");
?>
My problem -
When i login without checking remember me it logs me in fine and then when i close my browser and open it again it logs me out BUT when i do check remember me and login it doesn't take me to the user_area page it just stays on the login page and looks as if it has just refreshed. So my question is could someone help me with my problem, i want it so that if the user check's remember me it then logs in and keeps the user logged in, any help will be appreciated.