Jump to content

[SOLVED] Storing Globals


blackcell

Recommended Posts

Ok. So if you start session() this creates the session.  When is the session destroyed or released? When the user closes the browser or leaved the site?  I have a feeling it works like htaccess but not for sure.

 

Also, do you have to start session() on each page to recall the users session?

Link to comment
Share on other sites

like htaccess yes, all data is removed once the browser has been closed as the session id is lost.

 

every page using session variables needs session_start() first, and each page after the START page needs a $_GET['PHPSESSID'] variable passed TO it. you would use the php CONSTANT: SID, to input the session id.

 

ex.

 

page1.php

<?php
session_start();
$_SESSION['Number'] = $_GET['Number'];
echo("Page 1 Number Is: ".$_SESSION['Number']."<BR>".
    "<A href=\"page2.php?PHPSESSID=".SID."\">Next Page</a>"
);
?>

 

page2.php

<?php
session_start();
$_SESSION['Number'] = $_GET['Number'];
echo("Page 2 Number Is: ".$_SESSION['Number']."<BR>".
    "<A href=\"page3.php?PHPSESSID=".SID."\">Next Page</a>"
);
?>

 

page3.php

<?php
session_start();
echo("Page 3 Number Is: ".$_SESSION['Number']."<BR>".
    "<A href=\"page4.php?PHPSESSID=".SID."\">Next Page</a>"
);
?>

Link to comment
Share on other sites

  • 1 month later...

If you pass and grab just the session ID will the entire $_SESSION array be available or do you have to pass all of them?

 

I am trying to use this to store a user unique ID to an an array cell. After the login I will test at the beginning of each page for that array cell to have a value and if it doesn't they are redirected to the login page.

Link to comment
Share on other sites

Blackcell you can easily do this, $_SESSION variables are stored in an (encrypted?) file on the server.

 

the SESSION 'ID' is maintained byt the name of the file on the server and a cookie/query_string on the client's browser.

 

Every variable in the $_SESSION variable/array will be available as long as the clients browser can gived a valid Session ID, either by Cookie or URL Query.

----

Bear in mind Sessions do not last forever and not even dynamic lifetimes (cannot set them per user blah blah as far as i know of).

 

To keep users logged in after a week or so you will need a cookie (there may be other ways like hardware identifiers with java etc but thats probably overkill), Don't use IP Addresses.

 

Hope this helps,

Link to comment
Share on other sites

Ok cool, now I am using this to store sessions variables now.  I was using:

<?php
//Used to ensure uniqueness while moving through the site.
$tier1 = 0;
define("GSID",$tier1);
define("USER",$tier1++);
define("INFO1",$tier1++);
define("INFO_SO_ON",$tier1++);

//Then I the user will log on "code is not here"
//session_start(); happens at the beginning of every page.

//index.php
$queriedUID = 1; //For example
$_SESSION[GSID] = $queriedUID;  //<----This DOES NOT work.
$_SESSION['GSID'] = $queriedUID; //<----This WORKS

//I am not passing any variables through the URL

//main.php
echo $_SESSION[GSID]; //<----This DOES NOT work.
echo $_SESSION['GSID'];//<----This WORKS
?>

 

Point being I am trying to define constants as number and increment those numbers. As I list it through the definition of the constant.This way I do not have to bother with organization but I know everything is unique, therefore I can call something in the $_SESSION array with something a little easier to remember in the Index.  Some reason this isn't working but if I use just words in quotes the way it is done most of the time, it works.

 

You might be saying why not just use the words? Well when I use defined constants it can be rolled into one file, and provide a reference point instead of digging through lines of code to see what I used for something.

Link to comment
Share on other sites

It is fine to use quotes I just wanted to know what causes it not to work.

 

I wanted to use constants so that there would be a standard index for every value. This will eventually be a more than one person project that deals heavily with loading a users values from a database into session variables.  I wanted to make it so that everyone would know which values to use based on constants and they could reference a particular page to find that name.  So its no biggy.  I thought it looked cooler to  :P(lame I know)

Link to comment
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.