Jump to content

Sessions


SirChick

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/64396-sessions/
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/64396-sessions/#findComment-321068
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/64396-sessions/#findComment-321083
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.