0

Hi All,
I have developed php login system.
If user successfully logged in system will create a session call, $_SESSION['username'];
Now I want to redirect user, if not logged in when try to view specific page.
Please help me to write a code.
I already wrote a code,
but it is not working.

if(empty($_SESSION['username'])){
    header ("Location : login.php");
    }

Edited by jKidz

3
Contributors
5
Replies
23
Views
3 Years
Discussion Span
Last Post by tapananand
0

The header command needs to be ran before any other output (read the docs on header) so if you have HTML tags, comments, etc, before that code segment it won't run.

0

The header command needs to be ran before any other output (read the docs on header) so if you have HTML tags, comments, etc, before that code segment it won't run.

No. I wrote it very first

Edited by jKidz

0
<?php
session_start();
if(!isset($_SESSION['username']))
{
    echo "<script type=\"text/javascript\">window.location.href='login.php';</script>" ;
}
?>

Write this at the very top of your page.

0

Sorry! Some bug caused this multiple posting or may be my slow internet.

I think you are trying to increase your post counter

0

No that's not the case here. I clicked submit multiple times because after I clicked the button it freezed and the post didn't become permanent. So I refreshed the page and redid the whole thing. This happened multiple times unitil I saw that I created a mess.
By the way, did you try my solution??

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.