newbie2php Posted September 24, 2008 Share Posted September 24, 2008 Can anyone tell me where i can find a simple script that looks to see if a user is logged in and if they are change a login link to logout Here is the code that i put to start my session if ($loggedin == true) { //set the login session variables, uname and logged session_start(); //optionally setting sessions as well as cookies. $HTTP_SESSION_VARS['username'] = $u; $HTTP_SESSION_VARS['logged'] = true; // setcookies //if the user wants us to remember they are logged in... if (isset($HTTP_POST_VARS['saved']) && $HTTP_POST_VARS['saved'] == "yes") { //set cookies here. set for 1 week setcookie("checked", $HTTP_POST_VARS['saved'], time()+604800); setcookie("name", $u, time()+604800);// 1 week setcookie("pass", $p, time()+604800);// 1 week } else { // remove cookies by setting expiration to time in the past setcookie("checked", "", time()-1000); setcookie("name", "", time()-1000); setcookie("pass", "", time()-1000); } // send the successfully logged on visitor on their way... header("Location:protected.php"); exit; If anyone can help in anyway that would be great Link to comment https://forums.phpfreaks.com/topic/125630-login-logout-script/ Share on other sites More sharing options...
jonsjava Posted September 24, 2008 Share Posted September 24, 2008 seeing how I don't use cookies, I'm adding a single item, so I can write the code I know best: if ($loggedin == true) { //set the login session variables, uname and logged session_start(); $_SESSION['is_logged_in'] = true; //optionally setting sessions as well as cookies. $HTTP_SESSION_VARS['username'] = $u; $HTTP_SESSION_VARS['logged'] = true; // setcookies //if the user wants us to remember they are logged in... if (isset($HTTP_POST_VARS['saved']) && $HTTP_POST_VARS['saved'] == "yes") { //set cookies here. set for 1 week setcookie("checked", $HTTP_POST_VARS['saved'], time()+604800); setcookie("name", $u, time()+604800);// 1 week setcookie("pass", $p, time()+604800);// 1 week } else { // remove cookies by setting expiration to time in the past setcookie("checked", "", time()-1000); setcookie("name", "", time()-1000); setcookie("pass", "", time()-1000); } // send the successfully logged on visitor on their way... header("Location:protected.php"); exit; Now, all you will need to do is something like this: <?php if ($_SESSION['is_logged_in'] == true){ $login_nav_item = "<a href='logout.php'>Logout</a>"; } else{ $login_nav_item = "<a href='login.php'>Login</a>"; } Next, in your navbar, where they have the option to login/logout, tell it to print $login_lav_item in that spot. Link to comment https://forums.phpfreaks.com/topic/125630-login-logout-script/#findComment-649529 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.