Jenz
Jenz

Reputation: 8369

Page not getting redirected when sessions are not set - PHP

In home.php page, if session is not set I want to redirect to index page. But it is not getting redirected. Session will be set only if I login to the project. However, currently I am not logged in, so sessions are not set.

require_once 'header.php';
if(!isset($_SESSION['uid']) && !isset($_SESSION['uname']))
{    
   //echo "hai";                       // works
   header("location:index.php");
   exit;
} 
echo "hello";   // does not work

In header.php page I am starting session like

if(!isset($_SESSION)){
    session_start();
}

What I am doing wrong? Can anyone help me to find the problem?

Thanks in advance.

Upvotes: 0

Views: 129

Answers (3)

Antoan Milkov
Antoan Milkov

Reputation: 2228

Can you try like that and tell me how it is working:

require_once 'includes/header.php';
if(!isset($_SESSION['US_Id']) && !isset($_SESSION['US_UName']))
{    
   header("Location: /index.php");
   exit;
} 

Be sure to remove all the symbols (even the empty spaces) before the opening tag <?php and after the closing tag ?> in both your file and the header.php.

Having anything there will make impossible for the headers to be changed.

Upvotes: 1

Bijan
Bijan

Reputation: 8586

Your issue is that you are formatting wrong and you cannot use a relative path

Change it to:

header("Location: http://yoursite.com/index.php");

Upvotes: 0

turtle
turtle

Reputation: 1619

The correct format to redirect is via header is :

header('Location: '.$newURL);

Uppercase L

Upvotes: 2

Related Questions