Jump to content

Problem with Session Variable Passing


sudip_dg77

Recommended Posts

Hi,

 

I am new to php. I am trying to build a website where the users can login and navigate to various pages after the login. But I am facing some issues with setting up sessions. After the user provides his user id and password in the login form and presses on the "login" button the following code executes:

 

<?php
session_start();
?>

 

After the above code there is some HTML code and then again some php code which is as below:

 

<?php
$user_id = $_POST['user_id'];
$password = $_POST['password'];
$newline = "<br />";
$space = " ";

$myDatabase = "kuuja_business";


$con = mysql_connect("localhost:3306","kuuja_admin","admin1");

if (!$con)

  {

  die('Could not connect to Database: ' . mysql_error());

  }



@mysql_select_db($myDatabase, $con) or die("Unable to select database");

$result_user = mysql_query("SELECT user_id FROM user WHERE user_id = '$user_id'");
$row1 = mysql_fetch_array($result_user);
$user = $row1['user_id'];


$result_password = mysql_query("SELECT password FROM user WHERE password = '$password'");
$row2 = mysql_fetch_array($result_password);
$pass = $row2['password'];

$result_fname = mysql_query("SELECT fname FROM user WHERE user_id = '$user_id'");
$row3 = mysql_fetch_array($result_fname);
$name = $row3['fname'];

if ($user)
{
if ($pass)
{

	$_SESSION['user'] = $user;

                         echo "Your User Id is : $user".$newline.$newline;

	echo " Hello $name,".$newline.$newline;

	echo " Welcome to the members area.".$newline.$newline;

	echo " We hope you will enjoy your stay.";

}
else
{

	echo "Password does not match, please go back and retry.";
    		session_destroy();
}
}
else
{
echo "User Id does not match, please go back and retry.";
  	session_destroy();


}

mysql_close($con);

?>

 

 

Till this part the code executes ok. But once the user is successfully logged in I am trying to pass on the session variable on to the other pages where the user might navigate to on clicking the various links on the members area. Like there is a settings page insdie the members area for which I have put a small piece of test code which should be executed once the "Settings" link in the memebrs area is clicked. Here is that test code:

 

<?php
if (!isset($user)) 
{
echo 'Session is not set';
}
else
{
echo "$user is set";
}
?>

 

But always the the output on this "Settings" page is "Session is not set".

 

Can anyone please help me??

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/88065-problem-with-session-variable-passing/
Share on other sites

Here is an example of what I'm using on my website

 

I keep a track if the user is French or English, so I have a language Session variable

 

session_start();

 

$lang= $_SESSION['lang'];

 

The user is using a drop down for english or French

 

<select name="lang">

                <option value= "1" <?php if($_SESSION['lang']==1){echo 'selected="selected"';} ?>> English</option>

                <option value= "2" <?php if($_SESSION['lang']==2){echo 'selected="selected"';} ?> >Francais</option>

</select>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.