Welcome to Dream.In.Code
Become a PHP Expert!

Join 149,913 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,170 people online right now. Registration is fast and FREE... Join Now!




Propagating Session Variables

 
Reply to this topicStart new topic

Propagating Session Variables, What is that for?

max302
16 Nov, 2006 - 07:44 PM
Post #1

Proud supporter of the lulz
Group Icon

Joined: 5 Mar, 2006
Posts: 1,281


Dream Kudos: 200
My Contributions
So when a session is started, the server handles all the session information right?
So what is the use of passing out a session id on the url for every page? For passing that off, what is the superglobal for session id? Something like $_SESSION['id']?
Also, if you have to pass on the session id for every page, is there a function for getting the session ID.

I known that these are very basic questions, but all tuts that I found on Google where very basic, maybe a bit too much.

Thanks for the help.
User is offlineProfile CardPM
+Quote Post

BetaWar
RE: Propagating Session Variables
17 Nov, 2006 - 05:14 PM
Post #2

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,293



Thanked: 101 times
Dream Kudos: 1275
My Contributions
Okay, the session id for every page must be done with registering an IP and session variables into a database. From there have the site pull all session variables from the database where the IP is the same. That will allow you to get session variables to direct to each and every page.
That way is very common, but there are some easier ways to do it. Try using a dynamic paging system and have the index.php file create a session, then have the index simply include the necessary files into the content of the page. The script goes something like this:

CODE

<?php
//CREATES SESSION
SESSION_START();

//CHECKS IF SESSION IS ALREADY CREATED
if(isset($_SESSION[id])){
//IF SO IT UPDATES THE USERNAME
$username = $_SESSION[user];
}

//SOME MORE CODE GOES HERE
//GET TO THE CONTENT AREA OF THE PAGE

if(isset($_GET[act])){
//INCLUDES THE CORRESPONDING PAGE FOR THE ACT
include $_GET[act] . '.php';
}
else{
//IF NOT SET IT INCLUDES THE HOMEPAGE
  include'home.php';
}

//SOME MORE CODE GOES HERE TO FINISH THE PAGE UP
?>


I hope that that was of some help.
User is offlineProfile CardPM
+Quote Post

max302
RE: Propagating Session Variables
18 Nov, 2006 - 02:43 PM
Post #3

Proud supporter of the lulz
Group Icon

Joined: 5 Mar, 2006
Posts: 1,281


Dream Kudos: 200
My Contributions
Some stuff still is obscure. Here is a part of my login script.
CODE

$login_query = mysql_query("SELECT passwd FROM sqdbs_peoples WHERE first_name = '$_POST['first_name']' AND second_name = '$_POST['second_name']' AND last_name = '$_POST['last_name']' AND dnum = '$_POST['dnum']'");

$passhash = mysql_result($login_query,"1","passwd");

if (isset($passhash))
{
  if (md5($_POST['passwd']) == $passhash)
   {
    start_session();
    $_SESSION['first_name'] = $_POST['first_name'];
    $_SESSION['second_name'] = $_POST['second_name'];
    $_SESSION['last_name'] = $_POST['last_name'];
    $_SESSION['dnum'] = $_POST['dnum'];
    $index_query = mysql_query("SELECT index FROM sqdbs_peoples WHERE first_name = '$_SESSION['first_name']' AND second_name = '$_SESSION['second_name']' AND last_name = '$_SESSION['last_name']' AND dnum = '$_SESSION[dnum]' ")
    $_SESSION['first_name'] = $_POST['first_name'];
    $_SESSION['first_name'] = $_POST['first_name'];
    $_SESSION['first_name'] = $_POST['first_name'];
   }
        
}
?>


On successful login, I set the session variables to match with the posted vars. From there, why and where do I have to put and get the session id to pass it on to some other page?

Do I have to re-init the session vars every time I load a page? Session vars are superglobal, so they should stay between pages right?

User is offlineProfile CardPM
+Quote Post

psykoprogrammer
RE: Propagating Session Variables
18 Nov, 2006 - 02:58 PM
Post #4

D.I.C Head
Group Icon

Joined: 9 Oct, 2006
Posts: 68


Dream Kudos: 50
My Contributions
Ok, check it. Session variables in PHP are actually pretty easy. The method I typically use is to have one page that initializes everything. This page is called on every other page. It would look something like this:

CODE

<?php

// preProcess.php

// Connect to database.
// etc... etc...

session_name("mySiteName");
session_start();

?>


Then, as an example, lets take your login page. It would look something like this.

CODE


if (isset($_SESSION["first_name"]))
{
   header("Location: index.php");
   exit;
}


$login_query = mysql_query("SELECT passwd FROM sqdbs_peoples WHERE first_name = '$_POST['first_name']' AND second_name = '$_POST['second_name']' AND last_name = '$_POST['last_name']' AND dnum = '$_POST['dnum']'");

$passhash = mysql_result($login_query,"1","passwd");

if (isset($passhash))
{
  if (md5($_POST['passwd']) == $passhash)
   {
    start_session();
    $_SESSION['first_name'] = $_POST['first_name'];
    $_SESSION['second_name'] = $_POST['second_name'];
    $_SESSION['last_name'] = $_POST['last_name'];
    $_SESSION['dnum'] = $_POST['dnum'];
    $index_query = mysql_query("SELECT index FROM sqdbs_peoples WHERE first_name = '$_SESSION['first_name']' AND second_name = '$_SESSION['second_name']' AND last_name = '$_SESSION['last_name']' AND dnum = '$_SESSION[dnum]' ")
    $_SESSION['first_name'] = $_POST['first_name'];
    $_SESSION['first_name'] = $_POST['first_name'];
    $_SESSION['first_name'] = $_POST['first_name'];

    session_write_close();
    header("Location: index.php");
    exit;
   }
        
}


On every page I include the "preProcess.php" file to start the session. If the page requires the user to be logged in, you check for a session variable to be set. If it isn't, redirect to the login page. Please note that I am using "header" to redirect. If you've already sent out page content, headers will already be sent, an error will occur, and the redirect will not happen. To remedy this you can turn on "output buffering". Also be aware, however, that you'll need to force your session to write to disk before redirecting by using "session_write_close()".
User is offlineProfile CardPM
+Quote Post

max302
RE: Propagating Session Variables
18 Nov, 2006 - 03:39 PM
Post #5

Proud supporter of the lulz
Group Icon

Joined: 5 Mar, 2006
Posts: 1,281


Dream Kudos: 200
My Contributions
So in short, I can just start a session, then not pass any session id in the bar at all and not have to reinit session variables? Sorry, I'm really not getting this.

So lets say that we have a user, bob, who logs in. Somewhere in the login page, this is set:
CODE

$_SESSION['name'] = $_POST['name'];   // In this case equals "bob"


Now, after the login page, bob is redirected to the home, where a personalized message is supposed to be printed. Will the home page be able to access the $_SESSION['name'] variable declared in the previous page?
CODE

echo "Hello, '$_SESSION['name']'";


Now if this is true, then this means that I can just declare all my session stuff at login, then just end_session(). Correct me if I'm wrong.
User is offlineProfile CardPM
+Quote Post

psykoprogrammer
RE: Propagating Session Variables
18 Nov, 2006 - 09:22 PM
Post #6

D.I.C Head
Group Icon

Joined: 9 Oct, 2006
Posts: 68


Dream Kudos: 50
My Contributions
You can indeed declare all your session variables at login (at least in PHP 4+). And as long as each page uses session_start(), you are set. This command simply ties the session file and the client cookie and set the superglobal variable $_SESSION to contain all session variables set.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 02:08PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month