bncplix Posted February 14, 2009 Share Posted February 14, 2009 I have been trying everything and googling for the last hour to find this, and I can not. I am using sessions in php, one file validates the login and sets the session, like so: session_register("name"); Now in another file, Im using session_start(); to be able to check I also have this: if(!session_is_registered(name)){ To ensure i am online One problem, How can i get the name that its session is logged in with? I just cant figure it out and I hope somebody knows! Quote Link to comment https://forums.phpfreaks.com/topic/145149-php-sessions-get-user-name/ Share on other sites More sharing options...
Philip Posted February 14, 2009 Share Posted February 14, 2009 use: <?php session_start(); $_SESSION['name'] = 'my name'; echo $_SESSION['name']; ?> session_register is deprecated Quote Link to comment https://forums.phpfreaks.com/topic/145149-php-sessions-get-user-name/#findComment-761837 Share on other sites More sharing options...
bncplix Posted February 14, 2009 Author Share Posted February 14, 2009 use: <?php session_start(); $_SESSION['name'] = 'my name'; echo $_SESSION['name']; ?> session_register is deprecated But that dosn't work accross the files (at least not for me) Like, if that gets set in checklogin.php, i cant read it in index.php Quote Link to comment https://forums.phpfreaks.com/topic/145149-php-sessions-get-user-name/#findComment-761838 Share on other sites More sharing options...
Philip Posted February 14, 2009 Share Posted February 14, 2009 You must call session_start() at the top of the main pages. Example, I have these pages: index.php login.php logout.php news.php Each, of those must have session_start() at the top in order to check for a session. Quote Link to comment https://forums.phpfreaks.com/topic/145149-php-sessions-get-user-name/#findComment-761839 Share on other sites More sharing options...
Maq Posted February 14, 2009 Share Posted February 14, 2009 use: session_start(); $_SESSION['name'] = 'my name'; echo $_SESSION['name']; ?> session_register is deprecated But that dosn't work accross the files (at least not for me) Like, if that gets set in checklogin.php, i cant read it in index.php You should be able to read it anywhere on your site that has session_start(); Maybe you're destroying the session somewhere and it's not retaining the name. Quote Link to comment https://forums.phpfreaks.com/topic/145149-php-sessions-get-user-name/#findComment-761840 Share on other sites More sharing options...
bncplix Posted February 14, 2009 Author Share Posted February 14, 2009 use: <?php session_start(); $_SESSION['name'] = 'my name'; echo $_SESSION['name']; ?> session_register is deprecated But that dosn't work accross the files (at least not for me) Like, if that gets set in checklogin.php, i cant read it in index.php You should be able to read it anywhere on your site that has session_start(); Maybe you're destroying the session somewhere and it's not retaining the name. I removed session_register("myusername"); session_register("mypassword"); And replaced with $_SESSION['myusername'] = $myusername; $_SESSION['mypassword'] = $mypassword; But now it dosnt even let me login, when i try it just stays at the login page and nothing else, and all the pages say I am not logged in Quote Link to comment https://forums.phpfreaks.com/topic/145149-php-sessions-get-user-name/#findComment-761843 Share on other sites More sharing options...
Philip Posted February 14, 2009 Share Posted February 14, 2009 Hmm, mind showing some code? Quote Link to comment https://forums.phpfreaks.com/topic/145149-php-sessions-get-user-name/#findComment-761845 Share on other sites More sharing options...
bncplix Posted February 14, 2009 Author Share Posted February 14, 2009 Hmm, mind showing some code? <?php ob_start(); $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name=""; // 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['myusername'] = $myusername; $_SESSION['mypassword'] = $mypassword; //session_register("myusername"); //session_register("mypassword"); $inTwoMonths = 60 * 60 * 24 * 1 + time(); setcookie('user', md5($myusername), $inTwoMonths); header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ob_end_flush(); ?> You can see i have session register commented out In my index page, I have session_start(); echo $_SESSION['myusername']; //dosnt print anything! if(!session_is_registered(myusername)){ echo 'You are not logged in'; }else{ Ohh and you can see i tried to setup cookies, but then that makes more problems and rather stay with only one system than having to have two constantly working, so ignore that part Quote Link to comment https://forums.phpfreaks.com/topic/145149-php-sessions-get-user-name/#findComment-761846 Share on other sites More sharing options...
Maq Posted February 14, 2009 Share Posted February 14, 2009 You need session_start() on every page you want to use sessions on... Quote Link to comment https://forums.phpfreaks.com/topic/145149-php-sessions-get-user-name/#findComment-761848 Share on other sites More sharing options...
bncplix Posted February 14, 2009 Author Share Posted February 14, 2009 You need session_start() on every page you want to use sessions on... Even the file that creates them? Ill try Edit: Wow i dont know why i didnt try that in the first place, such a waste of my time googling all that time Thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/145149-php-sessions-get-user-name/#findComment-761850 Share on other sites More sharing options...
Maq Posted February 14, 2009 Share Posted February 14, 2009 Yes, even the file that creates them... Quote Link to comment https://forums.phpfreaks.com/topic/145149-php-sessions-get-user-name/#findComment-761851 Share on other sites More sharing options...
Philip Posted February 14, 2009 Share Posted February 14, 2009 You need session_start() on every page you want to use sessions on... Even the file that creates them? Ill try Edit: Wow i dont know why i didnt try that in the first place, such a waste of my time googling all that time Thanks guys Yup. The only time you don't need it is in an included file. It should throw an error. Like, if I had index.php <?php session_start(); include 'functions.php'; //... echo $_SESSION['name']; ?> I wouldn't need to call session_start in functions.php, but I will in index.php However, if I didn't include functions.php and instead I went straight to that file or had my form posted there and wanted to create a session, I'd need to call session_start Does that make sense? Quote Link to comment https://forums.phpfreaks.com/topic/145149-php-sessions-get-user-name/#findComment-761852 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.