I’ve been stuck on a problem for the past 3 hours and its driving me mental in regards to PHP sessions. Iv moved on from session_is_registered to $_SESSION and it dont want to work.
See code below (is in all files right at very top on pages i ant to protect with login):
<?php
session_start();
if(!isset($_SESSION['user']))
header("location: login.php");
?>
On another page access.php I have a form with two input boxes and submit button to check input and cross reference it with db. When incorrect values are entered i get message “Invalid login” which is right.. But when i type correct details leads me back to login.php page asking to login.
I am saving session and checking details as follows:
<?php
session_start();
include("dbfile.php");
$sql = mysql_query("SELECT id FROM myTable WHERE user=$username AND pass=$pass");
if(mysql_num_rows($sql)==1){
$_SESSION['user'] = "Test";
header("location: account.php");
}else{
header("location: login.php");
}
?>
unfortunately i am on a shared server which has register_globals off but am not sure if that is the problem. if i echo the output of the session o nother pages i get nothing. Like its not even registering/ssaving the session.
I have cookies/js enabled on browsers. Infact, i have tested on 4 browsers but same result. Whos gonna be the life saver on this one..
Try
$_POST['username']and$POST['pass']in your query, the variables$usernameand$passdon’t exist ifregister_globalsare turned off. (Which they should be.)If you want to see whether sessions are working or not, try this:
On the second page view you should get a result on the screen. 🙂