SirChick Posted August 11, 2007 Share Posted August 11, 2007 I tried to make a session so that data can be passed across pages.. but its not working... im not entirely sure i have written it out perfectly. This is what i have: Login Page: $query = mysql_query("SELECT * FROM userregistration WHERE Username == '$Username' and Password == '$Password'"); if(mysql_num_rows($query) == 1){ $values = mysql_fetch_array($query); $id = $values['ID']; $_SESSION['logged'] = $id; Then on the page that you go to once logged in: $id = $_SESSION['logged']; $query = mysql_query("SELECT * FROM userregistration WHERE Userid='$id'"); $values = mysql_fetch_array($query); $UserId = $values["UserID"]; $HandMoney = $values["MoneyInHand"]; Basically what i did was this second bit of code is "included" in my html page so that it will echo $HandMoney when i call it. I either have my include wrong or i have my session wrong, either way the $HandMoney won't display on the page when i echo it. *Just to add - no page errors occurs Quote Link to comment https://forums.phpfreaks.com/topic/64396-sessions/ Share on other sites More sharing options...
Fadion Posted August 11, 2007 Share Posted August 11, 2007 just to ask, have u used session_start() in the beginning of each page? Quote Link to comment https://forums.phpfreaks.com/topic/64396-sessions/#findComment-321067 Share on other sites More sharing options...
php_tom Posted August 11, 2007 Share Posted August 11, 2007 You need to call PHP's session_start() before you can use the $_SESSION global array. You need to call it in every page, not just once. I.e., <?php $query = mysql_query("SELECT * FROM userregistration WHERE Username == '$Username' and Password == '$Password'"); if(mysql_num_rows($query) == 1){ $values = mysql_fetch_array($query); $id = $values['ID']; session_start(); $_SESSION['logged'] = $id; } ?> and <?php session_start(); $id = $_SESSION['logged']; $query = mysql_query("SELECT * FROM userregistration WHERE Userid='$id'"); $values = mysql_fetch_array($query); $UserId = $values["UserID"]; $HandMoney = $values["MoneyInHand"]; ?> Incidentally, if you don't call session_start(), I don't think it causes an error, PHP just assumes that you haven't declared $_SESSION yet, and prints nothing. Hope that helps. P.S. Remember to call session_destroy() in your log out page! Quote Link to comment https://forums.phpfreaks.com/topic/64396-sessions/#findComment-321068 Share on other sites More sharing options...
LiamProductions Posted August 11, 2007 Share Posted August 11, 2007 You have to put a session on each page. Only cookies can use it on one page. Quote Link to comment https://forums.phpfreaks.com/topic/64396-sessions/#findComment-321076 Share on other sites More sharing options...
SirChick Posted August 11, 2007 Author Share Posted August 11, 2007 sorry i should of added session start is already on the page that is meant to be displaying the data... perhaps its my include? If i have: <? include("include.php"); //this is the connection to server etc include("homeloginvariables.php"); //loads all the games variables so they can be echo'd on any page but session doesnt work ?> would the include still work for when i open up new <? ?> tags later on in the same page or does the include only include withing the first set of tags? Quote Link to comment https://forums.phpfreaks.com/topic/64396-sessions/#findComment-321083 Share on other sites More sharing options...
Fadion Posted August 11, 2007 Share Posted August 11, 2007 U can include stuff wherever u like. I dont see errors right now so dont know what to tell. Quote Link to comment https://forums.phpfreaks.com/topic/64396-sessions/#findComment-321086 Share on other sites More sharing options...
SirChick Posted August 11, 2007 Author Share Posted August 11, 2007 should i provide the whole code put together ? :S Quote Link to comment https://forums.phpfreaks.com/topic/64396-sessions/#findComment-321092 Share on other sites More sharing options...
Fadion Posted August 11, 2007 Share Posted August 11, 2007 maybe u should!! Quote Link to comment https://forums.phpfreaks.com/topic/64396-sessions/#findComment-321314 Share on other sites More sharing options...
jeffrydell Posted August 12, 2007 Share Posted August 12, 2007 sorry i should of added session start is already on the page that is meant to be displaying the data... perhaps its my include? You need 'session_start():' on BOTH pages, not just the one 'that is meant to be displaying the data' I hope this helps! JAR Quote Link to comment https://forums.phpfreaks.com/topic/64396-sessions/#findComment-321400 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.