Jump to content

How to register _Session[var] globally


bwspot
Go to solution Solved by jcbones,

Recommended Posts

I have auth page that auth user against php and later registers data from mysql querry into the session varriables like this:

 

//register sesssion authenticated
else {$_SESSION['authenticated']=true;
//register other values
$sessionid=session_id();
$_SESSION['phonenumber'] = mysql_result($result,0,'phonenumber');
$_SESSION['username']  = mysql_result($result,0,'username');
$_SESSION['userlevel']  = mysql_result($result,0,'userlevel');

                                           
//session_register("userlevel");
//session_register("email");
//session_register("sitename");
//session_register("ipaddr");

//session_start(); - old and replaced by below line

$session = array();
 

after all is done the user is redirected back to:

 

 header("Location: main.php?".Session_Name()."=".Session_ID());

 

and in main.php varriables are called like:

dbConnect("corporate");
$mysql = "select * from users WHERE phonenumber = $phonenumber";
$rs = mysql_query($mysql);
$row = mysql_fetch_array($rs);

echo "phone is";
echo $phonenumber;
echo "global phone is ";
echo $GLOBALS['phonenumber'];

 

 

Problem is that after register_global was removed from php above $phonenumber varriable is empty.

 

I have seen this link:

http://uk3.php.net/manual/en/function.session-register.php

but i have hard time understanding how to fix above.

 

Every page has a session_start() at the beggining.

 

Can anyone help how to fix my script with the least changes?

I am using php from time to time so forgive me my lack of expertise.

 

thx

Edited by bwspot
Link to comment
Share on other sites

I'm not seeing the point in the global phone number. You have the phone number set in session data ($_SESSION['phonenumber']). The only reason globals are pseudo useful is because variables can be used within class context without having to be passed in. If this phone number is a company number and will be constant then you'll be better off adding as below:

 

define( 'COMPANY_PHONE', 0145875581144 );

echo COMPANY_PHONE;

 

 

Sorry if I've misunderstood but you're doing the right thing moving away from globals.

Link to comment
Share on other sites

The phone number is pulled from the data based based on the username that authenticates.

So it is different for every user.

After the user is redirected the $phonenumber variable is empty.

 

 

the referenced paga says:

 

"Caution

This registers a global variable. If you want to register a session variable from within a function, you need to make sure to make it global using the global keyword or the $GLOBALS[] array, or use the special session arrays as noted below.

"

 

I tried to add:

global $phonenumber

but the varriable was still empty after redirection

 

Link to comment
Share on other sites

$_SESSION is a super global already.

 

If you notice, your above referenced link has a note:

 

 

notes-reject.gif Notes
Caution

If you want your script to work regardless of register_globals, you need to instead use the $_SESSION array as $_SESSION entries are automatically registered. If your script uses session_register(), it will not work in environments where the PHP directive register_globals is disabled. 

Link to comment
Share on other sites

There are two things you need to take care of in order to use session variables:

  • Always use session_start, before any content has been sent to the browser.
  • Store the data in the $_SESSION array directly, do not use session_register.
As long as you do both of these, and the user is not blocking cookies, sessions will work.

 

Forget that session_register exists: It has been outdated and "deprecated" for a long time now.

Edited by Christian F.
Link to comment
Share on other sites

i do have session_start() at the begining of the script and I commented out the session_register.

The part i dont understand is how to store the data in the session array ?

 

My php page contains:

 

session_start()

 

$_SESSION['phonenumber'] = mysql_result($result,0,'phonenumber');
$_SESSION['username']  = mysql_result($result,0,'username');
$_SESSION['userlevel']  = mysql_result($result,0,'userlevel');

 

then i am not sure if this is what i need to store above in the array?


$session = array();

 

if yes how do i read it later?

 

thx and sorry for stupid questions but i am new to all this stuff and dont programm that often.

Link to comment
Share on other sites

  • Solution

 

//start session:
session_start();
//add to session:
$_SESSION['iAmAPhReak'] = 'My string of session data';
 
//echo the data:
echo $_SESSION['iAmAPhReak'];
 
//dump the data:
echo '<pre>' . print_r($_SESSION,true) . '</pre>';
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.