How to check for login session and redirect to a page if visitor is not logged in or not registered in PHP ?

Posted By: KP On July 19, 2009 Under Internet, Other, Programming, Web Design, Web Master Categories


If you want to redirect your visitor to some page if he is not logged in then you can use below given codes. This is useful when you want user to register on your site before he reaches to download link page but the main thing is the redirect must be search engine friendly.So you don’t get any errors in crawling.

<?php session_start();

if ($_SESSION[‘username’] == ”) {
header (“Location: login.php”);
}?>

//here we are checking that if username variable of session is empty then redirect to login.php

Now when the user logins you have to set his username in username variable of session.

Precautions:

Don’t forget to start session.

Destroy session or empty username if you want user to login again.

Tips:

Instead of redirecting to other page you can add your register/login page on same page.

Replace

header (“Location: login.php”);

by the code you want to display on page.Remember it’s PHP use echo to print HTML codes e.g. echo “<form>”;



Share with Friends ( Sharing is Caring! )


Subscribe For E-mail Notification Of Article Updates

Enter your email address:

NOTICE:
DO NOT FORGET TO CLICK ON THE VERIFICATION LINK AFTER SUBSCRIBING,THE VERIFICATION MAIL WILL BE SENT TO YOU FROM "FeedBurner Email Subscriptions".
( What is this? Why I need to do this?)

Related Posts

One response. Wanna say something?

  1. vinay kesharwani
    Jul 27, 2009 at 09:28:40
    #1

    Nice post dude….keep posting this type of stuff!!!

    [Reply]

Post a Comment

OR