Jump to content

Session variables issue


brad_langdon

Recommended Posts

I have a process page that logs the user in...no problems there.

I have two session variables but only one is passing onto the next page.

 

 

// PROCESS PAGE

 

if($count==1){

$_session['client'] = $myusername;  // myusername is already defined from a form

$_session['login'] = 'TRUE'; // this variable passes through fine

    session_write_close();

header("location:http://www.vander.co.nz/login/loggedin.php");

}

 

 

// NEXT PAGE

 

<?

    if ($_session['login'] = 'TRUE') { echo ("Welcome"); }

   

      $result = $_session['client']; // supposed to echo the clients name stored in "$myusername"

      echo $result;

?>

 

The end result does echo the "Welcome" but not the client name. I have included a start session at the top of both pages and also ended the session before the header on the process page.

 

Really stuck and have researched with no success.

Link to comment
Share on other sites

Thanks for the reply.

 

I changed it to SESSION instead of session but still no luck.

$myusername has been set as it is the input from the form on a previous page.

 

I added

 ini_set ("display_errors", "1");
error_reporting(E_ALL); 

to the output page but nothing came up.

 

PS I am not a pro with php...

 

Link to comment
Share on other sites

The following line is not testing if the value is equal to 'TRUE' it is setting the variable listed to the sting 'TRUE' and testing if the result of that is true, which it is -

 

if ($_session['login'] = 'TRUE') { echo ("Welcome"); }

 

An equal comparison is two == signs. One = sign is an assignment operator.

Link to comment
Share on other sites

Now that I checked the first variable isn't working either...I changed the variable name in the process page so that it didn't match but it still prints as true on the results page.

 

Thank you so much for the help by the way...

 

PROCESS PAGE

 

<?php session_start();
ob_start();
$host="****"; // Host name
$username="****"; // Mysql username
$password="****"; // Mysql password
$db_name="login"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['client'] = $myusername; 
$_SESSION['login'] = "TRUE"; 
    session_write_close();
header("location:http://www.vander.co.nz/login/loggedin.php");
}
else {
    $_SESSION['login'] == "TRUE";
    session_write_close();
header("Location: http://www.vander.co.nz/index.php");
}

ob_end_flush();
?>

 

 

END PAGE

 


<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
	<link href="http://www.vander.co.nz/Images/Favicon.ico" rel="shortcut icon"  />
	<link href="http://www.vander.co.nz/css/main.css" rel="stylesheet" type="text/css" />
        <link href="http://www.vander.co.nz/css/scroll.css" rel="stylesheet" type="text/css" />
        <link href='http://www.vander.co.nz/css/contact.css' type='text/css' rel='stylesheet' media='screen' />
        <link href="http://www.vander.co.nz/css/prettyPhoto.css" rel="stylesheet" type="text/css" media="screen" charset="utf-8" />

	<script src="http://www.vander.co.nz/js/jquery.prettyPhoto.js" type="text/javascript" charset="utf-8"></script>
       	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
	<script src="http://www.vander.co.nz/js/jquery.pngFix.js" type="text/javascript" ></script>
	<script src="http://www.vander.co.nz/js/jquery.scrollable-1.0.2.js" type="text/javascript" ></script>
	<script src="http://www.vander.co.nz/SpryAssets/SpryEffects.js" type="text/javascript"></script>
        
        <script src='http://www.vander.co.nz/js/jquery.simplemodal.js' type='text/javascript'></script>
	<script src="http://www.vander.co.nz/js/contact.js" type='text/javascript'></script>

      	<meta http-equiv="Content-Type" ncontent="text/html; charset=utf-8" />
	<title>Vander - Branding & Design</title>
	<script type="text/javascript">
<!--
function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_effectAppearFade(targetElement, duration, from, to, toggle)
{
Spry.Effect.DoFade(targetElement, {duration: duration, from: from, to: to, toggle: toggle});
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
//-->
</script>
</head>


<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-8299099-1");
pageTracker._trackPageview();
} catch(err) {}</script>

<body id="Home" onload="MM_preloadImages('Images/Portfolio/PrevOver.png','Images/Portfolio/NextOver.png')">		

<?php include("../header.php"); ?>



<div id="content">
    
    	<?
    	if ($_SESSION['login'] == "TRUE") { echo ("Welcome"); }
    		$result = $_SESSION['client'];
	echo $result;

?>
   
   <div style="width:100%; height:56px; float:left;"></div>


        
</div>


<?php include("../footer.php"); ?>

</body>
</html>

Link to comment
Share on other sites

Add the following two lines of code immediately after your first opening <?php tag on both the page where you are setting the session variables and on the page where you are referencing the session variables -

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

 

If you already have some existing php code immediately after the opening <?php tag, move that code down to its own line and add the above two lines immediately after the <?php tag.

Link to comment
Share on other sites

string(6) just means the type of the variable is a string and it has length 6. ;)

 

Now you can take that line out and uncomment header. Thanks!

 

After that, in your END PAGE, can you modify the first line from:

<?php session_start(); ?>

 

with

<?php session_start(); var_dump($_SESSION['client']); ?>

 

Same deal. Post what it prints out.

Link to comment
Share on other sites

Add the following two lines of code immediately after your first opening <?php tag on both the page where you are setting the session variables and on the page where you are referencing the session variables -

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

 

If you already have some existing php code immediately after the opening <?php tag, move that code down to its own line and add the above two lines immediately after the <?php tag.

 

That made no difference...

 

 

and for

string(6) just means the type of the variable is a string and it has length 6. ;)

 

Now you can take that line out and uncomment header. Thanks!

 

After that, in your END PAGE, can you modify the first line from:

<?php session_start(); ?>

 

with

<?php session_start(); var_dump($_SESSION['client']); ?>

 

Same deal. Post what it prints out.

 

the output was "NULL"

Link to comment
Share on other sites

Update to last post...

 

PFMaBiSmAd, the last piece of code yo asked me to write came up with this output...

 

"Notice: Undefined index: client in /var/www/vhosts/vander.co.nz/httpdocs/login/loggedin.php on line 76"

 

The code on line 76 is

	if ($_SESSION['login'] == "TRUE") { echo ("Welcome"); }

Link to comment
Share on other sites

I added

 ini_set ("display_errors", "1");
error_reporting(E_ALL); 

to the output page but nothing came up.

 

I would get into the habit of also including E_STRICT (to catch any strict standards notices as well):

error_reporting(E_ALL | E_STRICT);

(I'm guessing not related to the problem at hand..it's just nice to use, just in case of any other possible strict standard issues).

 

Side note: In one of the snippets (in the PROCESS PAGE), looks like you provided your actual database username and password.. I would recommend just substituting those values with asterisks or something. Shouldn't post the actual values...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.