The page isn't redirecting properly when I logout

262 Views Asked by At
<?php
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
include 'connection.php';

// session_start();

$user_check=$_SESSION['login_user'];
// SQL Query To Fetch Complete Information Of User

$ses_sql=mysqli_query($conn, "SELECT * FROM auth where email='$user_check'");
$row = mysqli_fetch_assoc($ses_sql);

$login_session =$row['email'];
$username =$row['name'];
$created_at =$row['created_at'];
$city =$row['city'];
$phone =$row['phone'];
$profile_photo =$row['photo'];

if(!isset($login_session)){
    mysqli_close($conn); // Closing Connection
    header('Location: index.php'); // Redirecting To Home Page
}
?>

I am commenting out the session_start() because I'm already starting the session in another php file that is included.

I can't understand why the the page isn't redirecting properly to the index.php file.

3

There are 3 best solutions below

0
On

Uncomment your session start

session_start();

Also make sure there is no html before you start your session

1
On

The header() function needs for Location: a absolute path, e.g. http://example.org.

Note: HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself: [...] [SOURCE]1

As of this your header function are not correct on most servers. I have one server who accepts relative URIs and one which needs absolute URIs for example.

You should use exit() after header to supress further execution of the script too.

0
On

I noticed you have a include in your code and you said the session_start() is there. Just make sure that include don't have any output in it. And session_start is actually the first thing called there.

More info: http://php.net/manual/en/function.header.php

http://php.net/manual/en/function.session-start.php