preventing from users opening other pages without login

php, security

Solution

You need to maintain a session for when users are logged in. At the point of login, set a session variable with their username or user ID. Then on the protected page, check if this session variable is present, before allowing them to view the page.

A basic example:

On successful login:

$_SESSION['userId'] = x;

On requesting the protected page:

if(!isset($_SESSION['userId']))
{
    // not logged in
    header('Location: login.php');
    exit();
}

More info about PHP Sessions.

Problem

I am very new to php (started learning it 4 days ago) and I got many questions... I hav a login page named index.php and after the login/verfication of username process, the user is taken to chat.php which has a link to some other php page called someOtherPage.php... My question is, how to prevent users from directly going to someOtherPage.php without logging in by typing this in the url like " www.nameOfMyWebsite.com/someOtherPage.php/ " And after this question is answered, I wish to ask few more on php... Is it valid to ask a slightly unrelated question(with respect to the title) in the same thread?

Original source

Related problems