Session variables not working

os242

New Member
Messages
46
Reaction score
0
Points
0
Hi there:

I am pasing variables with session variables through pages, in the fist page (Homepage) the user input his email address and I post it and collect it in a session variable; in a 2nd page I retake the session variable value and keep it in a variable through which I perform a query and the results of such query are also kept in a session variable; This method works in my testing server but not on X10hosting server, could anyone help me wit this? Thanks alot.

Homepage
$loginUsername=$_POST['Username'];
$_SESSION['GuestEMAIL']=$loginUsername;

2nd page
$loginUsername=$_SESSION['GuestEMAIL'];
$result = mysql_query("SELECT * FROM guests, projects WHERE GEMAIL='$loginUsername' AND GPJID=PID");
while($row = @mysql_fetch_array($result))
{
$GID=$row['GID'];
$_SESSION['GuestID']=$GID;
$PID=$row['PID'];
$_SESSION['ProjectID']=$PID;
}
echo $_SESSION['GuestID'];
...
 

Slothie

New Member
Messages
1,429
Reaction score
0
Points
0
You know, you just mind need to call session_start() before calling any $_SESSION variables
 

os242

New Member
Messages
46
Reaction score
0
Points
0
All pages have session_start() on but they do not pass variables at all.
 

mattura

Member
Messages
570
Reaction score
2
Points
18
Does something simple such as the following work?

1st page:
<?php
session_start();
$_SESSION['test']="hello";
header("Location: 2ndpage.php");
?>

2nd page:
<?php
session_start();
session_register(); //just in case...(should not be needed)
echo "Result:".$_SESSION['test'];
?>
 

os242

New Member
Messages
46
Reaction score
0
Points
0
I tried you example and works, but I do not know why my code is not working, it simply does not register the session variables and if it does not then pages cannot be loaded, I'll put all my code, could you please give me any hints? I'll enourmously appreciate it.

Home.php
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
session_start();
}
$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
$_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['Username'])) {
$loginUsername=$_POST['Username'];
$_SESSION['GuestEMAIL']=$loginUsername;
$password=$_POST['Password'];
$MM_fldUserAuthorization = "";
$MM_redirectLoginSuccess = "/Audit_Intro.php";
$MM_redirectLoginFailed = "/Login_Failed.htm";
$MM_redirecttoReferrer = false;
include('Connections/Conex.php');
mysql_select_db($database_Conex, $Conex);

$LoginRS__query=sprintf("SELECT GEMAIL, GPASS FROM guests WHERE GEMAIL='%s' AND GPASS='%s'",
get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc() ? $password : addslashes($password));

$LoginRS = mysql_query($LoginRS__query, $Conex) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);
if ($loginFoundUser) {
$loginStrGroup = "";

//declare two session variables and assign them
$_SESSION['MM_Username'] = $loginUsername;
$_SESSION['MM_UserGroup'] = $loginStrGroup;

if (isset($_SESSION['PrevUrl']) && false) {
$MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
}
header("Location: " . $MM_redirectLoginSuccess );
}
else {
header("Location: ". $MM_redirectLoginFailed );
}
}
include('counter.php');
?>

Audit_intro.php
<?php
//initialize the session
if (!isset($_SESSION))
{
session_start();
session_register();
include('Connections/Conex.php');
}

// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
$logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
//to fully log out a visitor we need to clear the session varialbles
$_SESSION['MM_Username'] = NULL;
$_SESSION['MM_UserGroup'] = NULL;
$_SESSION['PrevUrl'] = NULL;
unset($_SESSION['MM_Username']);
unset($_SESSION['MM_UserGroup']);
unset($_SESSION['PrevUrl']);

$logoutGoTo = "Home.php";
if ($logoutGoTo) {
header("Location: $logoutGoTo");
exit;
}
}
?>
<?php
include('Connections/Conex.php');
$loginUsername=$_SESSION['GuestEMAIL'];
$GLLI=date("Y-m-d G:i:s");
$_SESSION['GLLI']=$GLLI;

$result=mysql_query("UPDATE guests SET GLLI = '$GLLI' WHERE GEMAIL = '$loginUsername'");

$result = mysql_query("SELECT * FROM guests, projects WHERE GEMAIL='$loginUsername' AND GPJID=PID");
while($row = @mysql_fetch_array($result))
{
$GID=$row['GID'];
$_SESSION['GuestID']=$GID;
setcookie("GuestID","$GID");

$GNAME=$row['GNAME'];
$_SESSION['GuestNAME']=$GNAME;

$PID=$row['PID'];
$_SESSION['ProjectID']=$PID;
setcookie("ProjectID","$PID");

$PNAME=$row['PNAME'];
$_SESSION['ProjectNAME']=$PNAME;
}
?>
 

flinx

New Member
Messages
68
Reaction score
0
Points
0
Damn... 2 posts merged in 1. At the end you find the solution.

Just reposting your code with the PHP tags. Easier to read. ;)

Home.php
PHP:
 <?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}
$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['Username'])) {
  $loginUsername=$_POST['Username'];
  $_SESSION['GuestEMAIL']=$loginUsername;
  $password=$_POST['Password'];
  $MM_fldUserAuthorization = "";
  $MM_redirectLoginSuccess = "/Audit_Intro.php";
  $MM_redirectLoginFailed = "/Login_Failed.htm";
  $MM_redirecttoReferrer = false;
  include('Connections/Conex.php');
  mysql_select_db($database_Conex, $Conex);
  
  $LoginRS__query=sprintf("SELECT GEMAIL, GPASS FROM guests WHERE GEMAIL='%s' AND GPASS='%s'",
get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc() ? $password : addslashes($password)); 
   
  $LoginRS = mysql_query($LoginRS__query, $Conex) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
     $loginStrGroup = "";
    
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;	      

    if (isset($_SESSION['PrevUrl']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];	
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
include('counter.php');
?>

Audit_intro.php
PHP:
 <?php
//initialize the session
if (!isset($_SESSION)) 
{
  session_start();
  session_register(); 
  include('Connections/Conex.php');
}

// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
  $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
  //to fully log out a visitor we need to clear the session varialbles
  $_SESSION['MM_Username'] = NULL;
  $_SESSION['MM_UserGroup'] = NULL;
  $_SESSION['PrevUrl'] = NULL;
  unset($_SESSION['MM_Username']);
  unset($_SESSION['MM_UserGroup']);
  unset($_SESSION['PrevUrl']);
	
  $logoutGoTo = "Home.php";
  if ($logoutGoTo) {
	header("Location: $logoutGoTo");
    exit;
  }
}
?>
<?php
include('Connections/Conex.php');
  $loginUsername=$_SESSION['GuestEMAIL'];
  $GLLI=date("Y-m-d G:i:s");
  $_SESSION['GLLI']=$GLLI;
  
  $result=mysql_query("UPDATE guests SET GLLI = '$GLLI' WHERE GEMAIL = '$loginUsername'");
  
  $result = mysql_query("SELECT * FROM guests, projects WHERE GEMAIL='$loginUsername' AND GPJID=PID");
  while($row = @mysql_fetch_array($result))
  {
  $GID=$row['GID'];
  $_SESSION['GuestID']=$GID;
  setcookie("GuestID","$GID");
  
  $GNAME=$row['GNAME'];
  $_SESSION['GuestNAME']=$GNAME;
  
  $PID=$row['PID'];
  $_SESSION['ProjectID']=$PID;
  setcookie("ProjectID","$PID");
  
  $PNAME=$row['PNAME'];
  $_SESSION['ProjectNAME']=$PNAME;
  }
?>
Edit:
Instead of

PHP:
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}
use

PHP:
// *** Validate request to login to this site.
 session_start();
Likewise in the second script.
Not

PHP:
//initialize the session
if (!isset($_SESSION)) 
{
  session_start();
  session_register(); 
  include('Connections/Conex.php');
}

but

PHP:
//initialize the session
 session_start();
include('Connections/Conex.php');
 
Last edited:

os242

New Member
Messages
46
Reaction score
0
Points
0
Ok, I modify accordingly (get rid of if (!isset($_SESSION)) { and leaving just session_start();) however it does not work still. What else would it be? Thanks so much for your help!!!!!!!
 

flinx

New Member
Messages
68
Reaction score
0
Points
0
What exactly isn't working?
At the end of your first script what value is $_SESSION['GuestEMAIL'] holding?
At the beginning of your second script (after session_start()), what value is $_SESSION['GuestEMAIL'] holding?
 

os242

New Member
Messages
46
Reaction score
0
Points
0
Users access the webpage by entering their email address and password, I gather the email address inputed by them in a session variable which help me to access user data previously registered in the DB; so $_SESSION['GuestEMAIL'] of the first script (page) records the inputed user email and I attempt to recover that info by asigning $_SESSION['GuestEMAIL'] value to a variable in the second script (page) and perform a query to access further user information.
 

flinx

New Member
Messages
68
Reaction score
0
Points
0
I know, that's what you explained in the first post. But what is happening instead?
At the end of your first script what value is $_SESSION['GuestEMAIL'] holding?
At the beginning of your second script (after session_start()), what value is $_SESSION['GuestEMAIL'] holding?
Not in theory, but in reality. Do an echo. Tell me the result.
 

os242

New Member
Messages
46
Reaction score
0
Points
0
Ok I echo $_SESSION['GuestEMAIL'] on the second page and it does print the value posted by user, so it does pass the value to the second page through the session variable but because of an extrange reason, it does not perform the query based on the value of the session variable. Do you think there is a problem with my query? though it works well on my testing server.

<?php
include('Connections/Conex.php');
$loginUsername=$_SESSION['GuestEMAIL'];
$GLLI=date("Y-m-d G:i:s");
$_SESSION['GLLI']=$GLLI;

$result=mysql_query("UPDATE guests SET GLLI = '$GLLI' WHERE GEMAIL = '$loginUsername'");

$result = mysql_query("SELECT * FROM guests, projects WHERE GEMAIL='$loginUsername' AND GPJID=PID");
while($row = @mysql_fetch_array($result))
{
$GID=$row['GID'];
$_SESSION['GuestID']=$GID;
setcookie("GuestID","$GID");

$GNAME=$row['GNAME'];
$_SESSION['GuestNAME']=$GNAME;

$PID=$row['PID'];
$_SESSION['ProjectID']=$PID;
setcookie("ProjectID","$PID");

$PNAME=$row['PNAME'];
$_SESSION['ProjectNAME']=$PNAME;
}
?>
 

flinx

New Member
Messages
68
Reaction score
0
Points
0
Which one doesn't work? Both?
Do an echo of mysql_errno() and mysql_error() after each query.

PHP:
echo mysql_errno() . " - " . mysql_error();
 

os242

New Member
Messages
46
Reaction score
0
Points
0
it says: 1046 - No database selected, 1046 - No database selected on each query
Edit:
Ok, I introduced the line of: mysql_select_db($database_Conex, $Conex); before the query and it worked...

Thanks alot for all your help!!!
 
Last edited:
Top