I was able to locate a script that checks a user’s login credentials and then creates a session or denies login. Once the user is authenticated and the session is created, the page redirects to the home page. Unfortunately, I am getting an access denied error, and I think it’s because the session isn’t being created or carried over to the next page. Can someone help me out?
[php]<?php
//Start session
session_start();
//Include database connection details
require_once('config.php');
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
//Sanitize the POST values
$mid = clean($_POST['mid']);
$zip = clean($_POST['zip']);
//Input Validations
if($mid == '') {
$errmsg_arr[] = 'My ID missing';
$errflag = true;
}
if($zip == '') {
$errmsg_arr[] = 'ZIP Code missing';
$errflag = true;
}
//If there are input validations, redirect back to the login form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: login-form.php");
exit();
}
//Create query
$qry="SELECT * FROM users WHERE mid='$mid' AND zip='$zip'";
$result=mysql_query($qry);
//Check whether the query was successful or not
if($result) {
if(mysql_num_rows($result) == 1) {
//Login Successful
session_regenerate_id();
$member = mysql_fetch_assoc($result);
$_SESSION['SESS_MEMBER_ID'] = $member['member_id'];
session_write_close();
header("location: member-index.php");
exit();
}else {
//Login failed
header("location: login-failed.php");
exit();
}
}else {
die("Query failed");
}
?>[/php]
I’m not familiar with the $member variable, so I’m not sure if it has something to do with that? Does that need to be a field in my DB or is it just a function?
Upon successful login, the top of each page has the following start before the HTML begins…
[php]<?php
require_once(‘auth.php’);
?>[/php]
The auth.php file is…
[php]<?php
//Start session
session_start();
//Check whether the session variable SESS_MEMBER_ID is present or not
if(!isset($_SESSION['SESS_MEMBER_ID']) || (trim($_SESSION['SESS_MEMBER_ID']) == '')) {
header("location: access-denied.php");
exit();
}
?>[/php]