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

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.

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

<?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.

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

I think you are trying to increase your post counter

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??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.