conan318 Posted April 12, 2011 Share Posted April 12, 2011 Hi i have made a login in system for a website iam trying to make. after you log in im trying to display the members username via the $_session created in the check_login.php. but when i Echo or print_r the $_session all is get is "welcome array" its like its not passing any information via the $_session from page to page. here is my code thanks in advance. Check_login.php session_start(); // username and password sent from form $myusername=$_POST['myusername']; $mypassword=md5($_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["myusername"]==$myusername; $_SESSION["mypassword"]; header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?> login_success.php <? session_start(); if($_SESSION['myusername']="$myusername"){ header("location:main_login.php"); } Echo "welcome" . $_SESSION['$myusername']; ?> thanks Quote Link to comment https://forums.phpfreaks.com/topic/233459-php-_session-help-thanks/ Share on other sites More sharing options...
dcro2 Posted April 12, 2011 Share Posted April 12, 2011 I think you have your assignment/equal to operators confused a little. "=" is for assigning a variable. "==" is for checking if a something is equal to something else. I think your problem is right here: $_SESSION["myusername"]==$myusername; That only checks if the two are equal, it doesn't set it like you want. And here: if($_SESSION['myusername']="$myusername"){ That actually checks if $_SESSION['myusername'] was successfully set to $myusername. So try this for check_login.php <?php session_start(); // username and password sent from form $myusername=$_POST['myusername']; $mypassword=md5($_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["myusername"] = $myusername; //copy $myusername to $_SESSION['myusername'] $_SESSION["mypassword"] = $mypassword; //copy $mypassword to $_SESSION['mypassword'] header("location: login_success.php"); } else { echo "Wrong Username or Password"; } ?> and this for login_success.php <? session_start(); if($_SESSION['myusername'] != $myusername){ //if usernames don't match (is this what you wanted??) header("location: main_login.php"); } Echo "welcome " . $_SESSION['$myusername']; ?> By the way, where's $tbl_name come from exactly? PS: use tags for your code next time please. Quote Link to comment https://forums.phpfreaks.com/topic/233459-php-_session-help-thanks/#findComment-1200475 Share on other sites More sharing options...
conan318 Posted April 12, 2011 Author Share Posted April 12, 2011 hi thanks for your help... i just tried copied and pasted your code and tried it but it it just Echoed Welcome and nothing else. there is a sign up page which creates the username and password and small profile and picture for the user. now what I am wanting to is after the user logs in it will bring up there profile they created and saved to database. but if i cant access the user name from the SESSION i wont be able to work out which user has logged in. i did write the code in the dreamweaver cs4 editor and it was Charset utf 8 which i was reading could cause a problem but i have since remove the charset utf 8 and still cant get the username to display thru the SESSION. hope that makes sense thanks again Quote Link to comment https://forums.phpfreaks.com/topic/233459-php-_session-help-thanks/#findComment-1200505 Share on other sites More sharing options...
spiderwell Posted April 12, 2011 Share Posted April 12, 2011 i think he typoed in the echo statement, remove the $ from echo $_SESSION['$myusername'] so it is $_SESSION['username'] Quote Link to comment https://forums.phpfreaks.com/topic/233459-php-_session-help-thanks/#findComment-1200510 Share on other sites More sharing options...
dcro2 Posted April 12, 2011 Share Posted April 12, 2011 Sorry, missed that, but look at this line carefully: Echo "welcome " . $_SESSION['$myusername']; See the $ character like it's a variable? There's no $_SESSION['$myusername'] but there is a $_SESSION['myusername']. It wasn't a typo, it was in his original code Quote Link to comment https://forums.phpfreaks.com/topic/233459-php-_session-help-thanks/#findComment-1200512 Share on other sites More sharing options...
conan318 Posted April 12, 2011 Author Share Posted April 12, 2011 i have tried Echo "welcome " . $_SESSION['$myusername']; returns nothing also tried Echo "welcome " . $_SESSION['myusername']; also returns nothing i really don't understand why Quote Link to comment https://forums.phpfreaks.com/topic/233459-php-_session-help-thanks/#findComment-1200523 Share on other sites More sharing options...
conan318 Posted April 12, 2011 Author Share Posted April 12, 2011 i just removed this line from the login_success.php ("location: main_login.php"); is this line needed?? this is my frist my time working with php. Quote Link to comment https://forums.phpfreaks.com/topic/233459-php-_session-help-thanks/#findComment-1200550 Share on other sites More sharing options...
dcro2 Posted April 12, 2011 Share Posted April 12, 2011 You put it in there, not me. I don't know what you were trying to do. Why don't you just do a print_r($_SESSION) on that page to see if anything actually got set? Quote Link to comment https://forums.phpfreaks.com/topic/233459-php-_session-help-thanks/#findComment-1200556 Share on other sites More sharing options...
conan318 Posted April 12, 2011 Author Share Posted April 12, 2011 <? session_start(); if(!$_SESSION['myusername'] ){ //if usernames don't match (is this what you wanted??) header("location: main_login.php"); } Echo "welcome " . $_SESSION['myusername']; ?> Problem solved thanks for your help been a big help. Quote Link to comment https://forums.phpfreaks.com/topic/233459-php-_session-help-thanks/#findComment-1200557 Share on other sites More sharing options...
Pikachu2000 Posted April 12, 2011 Share Posted April 12, 2011 The tutorials you're using are obviously from phpeasystep.com, and are outdated. You should find a different source of information. Quote Link to comment https://forums.phpfreaks.com/topic/233459-php-_session-help-thanks/#findComment-1200558 Share on other sites More sharing options...
conan318 Posted April 12, 2011 Author Share Posted April 12, 2011 i have read many different tutorials from all over the place trying to make sense of it all, trying all different things till something works where is good place to for tutorials? I just discovered if i refresh the page it logs me out how can i keep the user logged if they refresh the page? Quote Link to comment https://forums.phpfreaks.com/topic/233459-php-_session-help-thanks/#findComment-1200576 Share on other sites More sharing options...
Pikachu2000 Posted April 12, 2011 Share Posted April 12, 2011 If you're just learning, I'd recommend buying a current book. Tutorials generally are for one specific process, and unless you already have a grasp of the language, you can't possibly know if the code they're pushing is any good or not until it's too late. Quote Link to comment https://forums.phpfreaks.com/topic/233459-php-_session-help-thanks/#findComment-1200580 Share on other sites More sharing options...
conan318 Posted April 12, 2011 Author Share Posted April 12, 2011 roger that i am currently studying web design and development on a 3 week break atm but wear going to start programing when we get back im trying to learn a few things b4 we start. Quote Link to comment https://forums.phpfreaks.com/topic/233459-php-_session-help-thanks/#findComment-1200606 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.