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>”;
Jul 27, 2009 at 09:28:40
Nice post dude….keep posting this type of stuff!!!
[Reply]