Page 1 of 1

php session help

Posted: Wed Oct 12, 2005 5:23 pm
by camhabib
Ok, here is the issue. I have a script to log in and define the users session as w/e it is in the database. The only problem is that when it sets the session name, if it even does set it, it doesn't stick. When this script is run, it comes out fine, no errors, and echo's "You are now logged in" when the correct password and username are provided. When you go back to the login screen, it should see that you are already logged in and direct you to a page, instead it just gives you the same message.

Code: Select all

<?php 

session_start(); 

if (!isset($_SESSION['name'])) { 
  if (isset ($_POST['username'])) {

  $person = $_POST['username']; 
  $pwd = $_POST['password']; 

  $dbusername="abc"; 
  $dbpassword="abc";
  $database="mysql"; 
  mysql_connect($database, $dbusername, $dbpassword); 
  mysql_select_db($database) or die("Unable to connect to database. Please contact the webmaster for further assistance."); 
  
  $query = "SELECT name FROM user_handle.users WHERE uname = '$person' AND  pword  =  '$pwd'";
  $result = mysql_query($query); 

   if (mysql_num_rows($result) != 1) { 
   echo ("Sorry but you are not authorize to view this page. Please check your username and password."); 
   									 } 
 		 else {
 		 $result = $_SESSION['usersess'];
 		 echo ("You are now logged in");
 		 	}
  }
  }
  else {
  require ('auth_index.html');
  		}

?>
Also, when this script is run, it returns the test session name but the whole echo having to do with the usersess session name just doesn't show up.

Code: Select all

<?php

session_start();
unset ($_SESSION['test']);
$_SESSION['test'] = 'test_session';
echo ("{$_SESSION['test']} is your test session name");
echo ("{$_SESSION['usersess']} is your test session name");

?>
Like I said, it finds the database, username, and everything fine, it just doesn't do anything with setting the session name. I’m really stumped on this one, I can’t figure out why it won’t set. Any help would be greatly appreciated.

Posted: Wed Oct 12, 2005 7:19 pm
by feyd
are you absolutely, 100% sure the session is getting set at all? Have you checked your error logs to see if anything shows up?

Posted: Wed Oct 12, 2005 7:27 pm
by camhabib
That’s the main thing, I have no idea if the script is actually setting the session name or not. I don't get ANY errors, I just checked all logs again. I wrote that small script to see if, a) sessions work and b) if sessions from other scripts will hold over. Sessions do work as the script returns "test_session" for the session defined in that script but NOTHING for the session defined in another script.

Even when I change the sript to

Code: Select all

$query = "SELECT name FROM user_handle.users WHERE uname = '$person' AND  pword  =  '$pwd'";
  $result = mysql_query($query); 

   if (mysql_num_rows($result) != 1) { 
   echo ("Sorry but you are not authorize to view this page. Please check your username and password."); 
   									 } 
 		 else {
 		 $result = $_SESSION['usersess'];
 		 echo ("Your session name is now: {$_SESSION['usersess']}");
 		 		}
it returns "Your session name is now: " when it should say Test after the :