Jump to content

Redirect someone thats not logged in?


Tom8001

Recommended Posts

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();
}

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.";
	}
 
 
}

?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.