sudip_dg77 Posted January 27, 2008 Share Posted January 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/88065-problem-with-session-variable-passing/ Share on other sites More sharing options...
shocker-z Posted January 27, 2008 Share Posted January 27, 2008 you need to use session_start(); at the top of every single page.. including any page inbetween even if your not wanting to use the variable on the page. Regards Liam Quote Link to comment https://forums.phpfreaks.com/topic/88065-problem-with-session-variable-passing/#findComment-450554 Share on other sites More sharing options...
kenrbnsn Posted January 27, 2008 Share Posted January 27, 2008 Actually, you don't need to use session_start() on pages where you're not using $_SESSION. Ken Quote Link to comment https://forums.phpfreaks.com/topic/88065-problem-with-session-variable-passing/#findComment-450563 Share on other sites More sharing options...
gasma1975 Posted January 27, 2008 Share Posted January 27, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/88065-problem-with-session-variable-passing/#findComment-450764 Share on other sites More sharing options...
revraz Posted January 27, 2008 Share Posted January 27, 2008 So what's the actual problem? Quote Link to comment https://forums.phpfreaks.com/topic/88065-problem-with-session-variable-passing/#findComment-450781 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.