rhale Posted November 5, 2009 Share Posted November 5, 2009 for some reason my session variables dont stay when i use require once on login_success.php and i get these errors. everything works properly when i use them on the checklogin.php but when i try to use them on other pages they dont. Notice: Undefined index: myusername in C:\wamp\www\login\checklogin.php on line 13 Notice: Undefined index: mypassword in C:\wamp\www\login\checklogin.php on line 14 Wrong Username or Password Notice: Undefined variable: _SESSION in C:\wamp\www\login\login_success.php on line 5 checklogin.php <?php $host="localhost"; //Host name $username="ryan"; // Mysql username $password="password"; // Mysql password $db_name="test"; // 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"); // username and password sent from form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; $sql="SELECT username and password FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); $accesslevels = mysql_query("SELECT accesslevel FROM $tbl_name WHERE username='$myusername' and password ='$mypassword'"); $accesslevel = mysql_fetch_assoc($accesslevels); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row //register user and accesslevel if($count==1 and $accesslevel['accesslevel']=='1'){ session_start(); $_SESSION['user'] = $myusername; $_SESSION['accesslevel'] = $accesslevel['accesslevel']; header("location:login_success.php"); } elseif($count==1 and $accesslevel['accesslevel']=='2'){ session_start(); $_SESSION['user'] = $myusername; print $_SESSION['user']; echo "welcome manager"; } elseif($count==1 and $accesslevel['accesslevel']=='3'){ session_start(); $_SESSION['user'] = $myusername; print $_SESSION['user']; echo "welcome admin"; } else { echo "Wrong Username or Password"; } ?> login_success.php <?php // Check if session is registered require_once 'checklogin.php'; print $_SESSION['user']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/180353-solved-global-session/ Share on other sites More sharing options...
waynew Posted November 5, 2009 Share Posted November 5, 2009 You're not placing session_start(); At the top of your pages. session_start() allows session variables to continue on from page to page. Quote Link to comment https://forums.phpfreaks.com/topic/180353-solved-global-session/#findComment-951421 Share on other sites More sharing options...
colap Posted November 5, 2009 Share Posted November 5, 2009 The session_start() will be the first line i think. Quote Link to comment https://forums.phpfreaks.com/topic/180353-solved-global-session/#findComment-951424 Share on other sites More sharing options...
rhale Posted November 5, 2009 Author Share Posted November 5, 2009 that worked thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/180353-solved-global-session/#findComment-951445 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.