Skip to content Skip to sidebar Skip to footer

If(!isset($_session['username'])) Causing Users To Be Redirected From Verify_login_form.php Back To Index.php

I have a login system on my website which takes the user from index.php to verify_login_form.php and then, if the Email/Username combination matches an account they are taken to ho

Solution 1:

All you have to do is create a session for the login. So here is the trick. Each time you move from index to home, you shoulkd check if the login session exists. If not, ask the user to login.

Check if a user is logged in

<?PHP
    session_start();

    if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {

        header ("Location: login.php");
    }
?>

Again, we first start a PHP session. The IF Statement that comes next is quite complex. But we're testing for two things: has a user session called login been set? And is this session a blank string?

!(isset($_SESSION['login']) && $_SESSION['login'] != '')

The first part is this:

!(isset($_SESSION['login'])

To check if a session is set you can use the inbuilt function isset. We're using the NOT operator before it. (The NOT operator is an exclamation mark.) So we're saying, "IF the session is NOT set". The session might be set, but may have a "1" in it. We also need to check if the session called login is a NOT blank string. If both of these things fail then we can redirect to the login.php page, as it means that the user is not logged in.

For every page in your site, if you have the above script at the top of your page, it will redirect a user if they are not logged in. That way, you can protect your pages from non-members. If they are logged in, they will be able to view the page.

Logging out If you have a look at the code for logout.php you'll see the following:

<?PHP
    session_start();
    session_destroy();
?>

This is all you need to log a user out: you start a session, and then issue the session_destroy command. All you need is a link to this page from anywhere on your site. The link would be something like this as your HTML:

<AHREF = logout.php>Log Out</A>

When the user clicks this link, they will be taken to the page with the code that destroys the session.

Post a Comment for "If(!isset($_session['username'])) Causing Users To Be Redirected From Verify_login_form.php Back To Index.php"