Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.