It's been years since I've done any simple PHP so forgive me for forgetting. I'm trying to create a function page called 'security.php' which restricts access to pages if no user is logged in.
I've added the following code at the begginning of all pages i want restricted:
require_once('security.php'); // restrict access to logged users
And for security.php I attempted the following code but it doesn't work. I tried to make it fetch information from the rows corresponding to the username logged in, if the amount of information = 0 then there must be no user logged in and the pageviewer is redirected to the login page. It gives me an error as you'll notice it's sending out headers too early. I also get the feeling I've over complicated it:
<?php
require_once('global.php'); // session start
require_once('conn.php'); // db connection
$username = $_SESSION['username'];
$username = mysql_real_escape_string($username);
$match = mysql_query("SELECT Username FROM tblusers WHERE username = '$username'")or die(mysql_error());
$row = mysql_fetch_array($match);
$security = count($row);
if ($security == 0 ) {
header("Location: login.php");
}else{
}
?>
Is there a simpler way of restricting access?
Thank you
This post has been edited by Sayid Ahmed: 09 September 2010 - 10:19 AM

New Topic/Question
Reply




MultiQuote






|