March 14, 201214 yr Hello all, yes its that time again. I have embarked on another attempt to further my knowledge and unfortunately, you guys face my questions. So, i have finished making the registration / login script. Which works as i wish. Here is what i am trying to achieve. - User logs in and is shown new content on the page. With this, the login bar in the header changes to a nav bar. - I also want it so that no matter what happens, they wont see the login bar again until they choose to log out. So, this is my login.php: <?php if (isset($_POST['loginbutton'])) { session_start(); // Start a new session require("conn.php"); // Get the data passed from the form $username = $_POST['username']; $password = $_POST['password']; // Do some basic sanitizing $username = stripslashes($username); $password = stripslashes($password); $sql = "select * from memberinfo where username = '$username' and password = '$password'"; $result = mysql_query($sql) or die ( mysql_error() ); if (mysql_num_rows($result) > 0) { $_SESSION['loggedIn'] = true; echo "<script>location.href='profile.php'</script>"; } else { $_SESSION['loggedIn'] = false; echo "<script>location.href='loginerror.php'</script>"; } } ?> then this is in my header: <div id="headerwrap"> <div id="logo">Social Network BETA</div> <?php if ($_SESSION['loggedIn'] = "false") { include 'includes/loginform.php'; } elseif ($_SESSION['loggedIn'] = "true") { include 'includes/usermenu.php'; } ?> </div> So as of now, if you login, the page refreshes, but i still see the login form. Any help on what i wish to achieve / point me to some info. Many thanks to all that have helped me so far!
March 14, 201214 yr I'm not too sure here as your code is abit vague, but if you're storing the session variables as a boolean shouldn't you also be checking them as a boolean? Also you don't need elseif in situations like this as there are only 2 possible answers. <div id="headerwrap"> <div id="logo">Social Network BETA</div> <?php if ($_SESSION['loggedIn'] == false) { include 'includes/loginform.php'; } else { include 'includes/usermenu.php'; } ?> </div> Also learn to use shorthand, see how much cleaner that is? <div id="headerwrap"> <div id="logo">Social Network BETA</div> <?php include ($_SESSION['loggedIn'] == false ? 'includes/loginform.php' : 'includes/usermenu.php'); ?> </div> Also are both codes in the same file? If not don't forget to include session_start(); at the beginning of all pages you're using sessions. Edited March 14, 201214 yr by Samus
March 15, 201214 yr Author Got it working Just a quick one, i want to do a check if there is a session exsiting when the user visits the index.php. if there is a session, it gets redirected to the profile page, if not, then it stays on the same page. this works with: <?php session_start(); if ($_SESSION['loggedIn'] == true) { echo "<script>location.href='profile.php'</script>"; } else { } ?> when i put it into my index.php page. However when logging out, i get the Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent error... im guessing this is because my header has the session start as well... i did a bit of research but still not quite sure whats causes this error. It all still works fine though. One last thing, whats the proper way to 'do nothing' in an if statement? or is leaving it blank acceptable?
March 15, 201214 yr Got it working Just a quick one, i want to do a check if there is a session exsiting when the user visits the index.php. if there is a session, it gets redirected to the profile page, if not, then it stays on the same page. this works with: <?php session_start(); if ($_SESSION['loggedIn'] == true) { echo "<script>location.href='profile.php'</script>"; } else { } ?> when i put it into my index.php page. However when logging out, i get the Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent error... im guessing this is because my header has the session start as well... i did a bit of research but still not quite sure whats causes this error. It all still works fine though. One last thing, whats the proper way to 'do nothing' in an if statement? or is leaving it blank acceptable? it means ur sending output before your calling session_start(); the session_start needs to be at the top of the page before any html or php is outputted
March 30, 201214 yr Got it working Just a quick one, i want to do a check if there is a session exsiting when the user visits the index.php. if there is a session, it gets redirected to the profile page, if not, then it stays on the same page. this works with: <?php session_start(); if ($_SESSION['loggedIn'] == true) { echo "<script>location.href='profile.php'</script>"; } else { } ?> when i put it into my index.php page. However when logging out, i get the Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent error... im guessing this is because my header has the session start as well... i did a bit of research but still not quite sure whats causes this error. It all still works fine though. One last thing, whats the proper way to 'do nothing' in an if statement? or is leaving it blank acceptable? Think it would be better to use php header too instead of javascript to redirect the user. Like so: <?php session_start(); if ($_SESSION['loggedIn'] == true) { header("location: profile.php"); } else { } ?>
March 31, 201214 yr Think it would be better to use php header too instead of javascript to redirect the user. Like so: <?php session_start(); if ($_SESSION['loggedIn'] == true) { header("location: profile.php"); } else { } ?> ur header redirect should have an exit(); below it to be sure the rest of the script cant run incase the redirect fails for some reason
March 31, 201214 yr ur header redirect should have an exit(); below it to be sure the rest of the script cant run incase the redirect fails for some reason Yes you are correct .
April 1, 201214 yr Surely that's down to poor planning from the developer. If a page needs to redirect, it's usually for a good reason, hence the exit. um yea obviously that's why i mentioned he should add the exit lol
Create an account or sign in to comment