Click to See Complete Forum and Search --> : restict page access


kproc
11-10-2006, 08:30 PM
Below is code that I use to insure a person has loged in before they access the page. at times it sends the user back to the index page. is there a better way to limit access to logged in users. I user $_session['user_id'] on all my pages to get the user_id. if there a better way to use this and simplify what I'm trying to do



<?php
/* Check User Script */
session_start(); // Start Session
include 'config/db.php';
//validate if user logged in
$user_id = $_SESSION['user_id'];

$sql_validate_user = mysql_query("SELECT * FROM users WHERE user_id = '$user_id'");
$validate_user = mysql_num_rows($sql_validate_user);

if(!$validate_user){
$msg .= '<div style="width:350px" id= "formmessage">';
$msg .= "You tryed to access a members only page. Please login or become a registered member to access that page!";
$msg .= '</div>';
include 'index.php';
exit();

}
?>