odysseusming Posted February 10, 2009 Share Posted February 10, 2009 I'm trying to create a simple PHP login, where the user logs in on the first page (login.php), and if the login is correct, is directed to the second page (page.php). I don't want the user to be able to access the second page without logging in, but I'm having trouble getting the code to work. login.php page code: <?php // Define your username and password $username = "a"; $password = "b"; # define a redirect url $redirect_url = "http://www.mydomain.com/page.php"; if (($_POST['txtUsername'] == $username) && ($_POST['txtPassword'] == $password)) { if (!$_SESSION) { session_start(); } $_SESSION['logged_in'] = "yes"; header("Location: $redirect_url"); die; } ?> <h2>Please Login</h2> <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <p> <br/> <label for="txtUsername">Username*</label> <br/> <input name="txtUsername" type="text" id="txtUsername" size="30" /> <br/> <label for="txtPassword">Password*</label> <br/> <input name="txtPassword" type="password" id="txtPassword" size="30" /> <br/> <br/> <input name="Submit" type="submit" value="Login" /> </p> </form> page.php page code: <?php if ($_SESSION['logged_in'] !== "yes") { header("Location: http://www.mydomain.com/login.php"); die; } ?> successful Quote Link to comment https://forums.phpfreaks.com/topic/144633-simple-php-login-with-redirect-problem/ Share on other sites More sharing options...
premiso Posted February 10, 2009 Share Posted February 10, 2009 <?php if ($_SESSION['logged_in'] != "yes") { header("Location: http://www.mydomain.com/login.php"); die; } ?> You were using the wrong operator check, use != instead of !== for this scenario and see if that helps you. Quote Link to comment https://forums.phpfreaks.com/topic/144633-simple-php-login-with-redirect-problem/#findComment-758944 Share on other sites More sharing options...
peranha Posted February 10, 2009 Share Posted February 10, 2009 you need session_start(); on the page.php code as well. Quote Link to comment https://forums.phpfreaks.com/topic/144633-simple-php-login-with-redirect-problem/#findComment-758947 Share on other sites More sharing options...
odysseusming Posted February 10, 2009 Author Share Posted February 10, 2009 Thanks - those changes got me to the "page.php" page, but I had to login twice? Any thoughts... Quote Link to comment https://forums.phpfreaks.com/topic/144633-simple-php-login-with-redirect-problem/#findComment-758955 Share on other sites More sharing options...
peranha Posted February 10, 2009 Share Posted February 10, 2009 Nothing in the code should make you login twice. Quote Link to comment https://forums.phpfreaks.com/topic/144633-simple-php-login-with-redirect-problem/#findComment-758958 Share on other sites More sharing options...
odysseusming Posted February 10, 2009 Author Share Posted February 10, 2009 Yeah - I can't figure it out. If I try to first go to the "page.php" page, it redirects me back to the "login.php" as it's supposed to - and then I can login successfully on the first try. But if I go straight to the "login.php" page, I have to login twice. Quote Link to comment https://forums.phpfreaks.com/topic/144633-simple-php-login-with-redirect-problem/#findComment-758960 Share on other sites More sharing options...
premiso Posted February 10, 2009 Share Posted February 10, 2009 if (!$_SESSION) { session_start(); } calling session_start(); at the top of the page without an if is "OK" to do. That is just extra code and is probably causing your issue. Remove the if then move session_start() to the top of your login page. Quote Link to comment https://forums.phpfreaks.com/topic/144633-simple-php-login-with-redirect-problem/#findComment-758964 Share on other sites More sharing options...
odysseusming Posted February 10, 2009 Author Share Posted February 10, 2009 Still requiring me to login twice. My code now looks like this: login.php page code: <?php session_start(); // Define your username and password $username = "a"; $password = "b"; # define a redirect url $redirect_url = "http://www.mydomain.com/page.php"; if (($_POST['txtUsername'] == $username) && ($_POST['txtPassword'] == $password)) { $_SESSION['logged_in'] = "yes"; header("Location: $redirect_url"); die; } ?> <h2>Please Login</h2> <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <p> <br/> <label for="txtUsername">Username*</label> <br/> <input name="txtUsername" type="text" id="txtUsername" size="30" /> <br/> <label for="txtPassword">Password*</label> <br/> <input name="txtPassword" type="password" id="txtPassword" size="30" /> <br/> <br/> <input name="Submit" type="submit" value="Login" /> </p> </form> page.php page code: <?php session_start(); if ($_SESSION['logged_in'] !== "yes") { header("Location: http://www.mydomain.com/login.php"); die; } ?> successful Quote Link to comment https://forums.phpfreaks.com/topic/144633-simple-php-login-with-redirect-problem/#findComment-758970 Share on other sites More sharing options...
premiso Posted February 10, 2009 Share Posted February 10, 2009 Please use [ code] [ /code] tags for posting code in (no initial space). <?php session_start(); // Define your username and password $username = "a"; $password = "b"; # define a redirect url $redirect_url = "http://www.mydomain.com/page.php"; if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == "yes") { header("Location: $redirect_url"); } if (($_POST['txtUsername'] == $username) && ($_POST['txtPassword'] == $password)) { $_SESSION['logged_in'] = "yes"; header("Location: $redirect_url"); die; } ?> A little redundant, but that should work to avoid logging in twice. Quote Link to comment https://forums.phpfreaks.com/topic/144633-simple-php-login-with-redirect-problem/#findComment-758977 Share on other sites More sharing options...
odysseusming Posted February 10, 2009 Author Share Posted February 10, 2009 thanks - but I pasted in that code and it's still requiring me to login twice... Quote Link to comment https://forums.phpfreaks.com/topic/144633-simple-php-login-with-redirect-problem/#findComment-758982 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.