Tom8001 Posted November 23, 2014 Share Posted November 23, 2014 Hi i am wondering how i can redirect someone that's not logged in? Link to comment https://forums.phpfreaks.com/topic/292664-redirect-someone-thats-not-logged-in/ Share on other sites More sharing options...
NotionCommotion Posted November 23, 2014 Share Posted November 23, 2014 Check whether they are logged on (i.e. is a session set, etc). If not, header("Location: goAway.php"); exit; http://php.net/manual/en/function.header.php Link to comment https://forums.phpfreaks.com/topic/292664-redirect-someone-thats-not-logged-in/#findComment-1497428 Share on other sites More sharing options...
QuickOldCar Posted November 23, 2014 Share Posted November 23, 2014 By checking the session if a user is logged in, if not do a header or even meta refresh redirect Header must be used before any output to the browser. if(!$_SESSION['logged_in']){ header('Location: http://www.example.com/'); die(); } or if(!$_SESSION['logged_in']){ echo "<meta http-equiv='refresh' content='0;http://www.example.com/' />"; echo die(); } Link to comment https://forums.phpfreaks.com/topic/292664-redirect-someone-thats-not-logged-in/#findComment-1497429 Share on other sites More sharing options...
Tom8001 Posted November 23, 2014 Author Share Posted November 23, 2014 This is my code <?php session_start(); require 'connect.php'; if(isset($_POST['submit'])) { $username = $_POST['username']; $password = $_POST['password']; //Prevent hackers from using SQL Injection $username = stripslashes($username); $password = stripslashes($password); $username = mysql_real_escape_string($username); $password = mysql_real_escape_string($password); $sql = "SELECT * FROM $tbl_name WHERE username='$username' AND password='$password'"; $result = mysql_query($sql); $count = mysql_num_rows($result); $row = mysql_fetch_assoc($result); $user_level = $row['user_level']; if($count == 1) { $_SESSION['username']; $_SESSION['password']; $_SESSION['loggedIn'] = true; header("Location: index.php"); } else { echo "Please check the username and password you entered is correct."; } if($row['user_level'] == 0) { //Do Nothing } else if($row['user_level'] == -1) { header("Location: banned.php"); } else if($row['user_level'] < -1) { header("Location: banned.php"); die(); } else if($row['user_level'] == 1) { header("Location: admin.php"); } else if($row['user_level'] == 2) { echo "Moderators are currently disabled."; } } ?> Link to comment https://forums.phpfreaks.com/topic/292664-redirect-someone-thats-not-logged-in/#findComment-1497430 Share on other sites More sharing options...
Tom8001 Posted November 23, 2014 Author Share Posted November 23, 2014 But that code is for my login.php page i want to make it so users need to be logged in to view the index page Link to comment https://forums.phpfreaks.com/topic/292664-redirect-someone-thats-not-logged-in/#findComment-1497432 Share on other sites More sharing options...
QuickOldCar Posted November 23, 2014 Share Posted November 23, 2014 Then add this to the top of the page. <?php session_start(); if(!$_SESSION['loggedIn']){ header('Location: http://www.example.com/login.php');//take to login or registration page? die(); } ?> Link to comment https://forums.phpfreaks.com/topic/292664-redirect-someone-thats-not-logged-in/#findComment-1497436 Share on other sites More sharing options...
Tom8001 Posted November 23, 2014 Author Share Posted November 23, 2014 Thanks man it worked i figured out the problem the session was still active, my logout code was session_unset(); session_destory(); header("Location: login.php"); I changed this to session_start(); session_destroy(); header("Location: login.php"); And that fixed the problem Thanks for your help Link to comment https://forums.phpfreaks.com/topic/292664-redirect-someone-thats-not-logged-in/#findComment-1497439 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.