Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

Session missing in passing thru pages

General discussions related to php

Moderators: macek, egami, gesf

Session missing in passing thru pages

Postby ransome » Wed Jan 15, 2003 5:42 pm

Dear all,

In my project, a login page is set as index.php which check the login user and creat appropiate session for various user by :

session_start;
:
check user statements
:
$super = $username;
session_register("super");

then the page jump to next page and so on, the sessionID is passed thru quite smoothly.

But the session lost when I try to made insertion or updating database when it pass to a html form. I have code the necessary codes for using the session but whatever I do, I check the session string is empty !!

How can I fix the problem ?



With regards,

Ransome :cry:
ransome
New php-forum User
New php-forum User
 
Posts: 7
Joined: Tue Jan 14, 2003 6:36 pm

Re: Session missing in passing thru pages

Postby WiZARD » Thu Jan 16, 2003 12:07 am

At each page you do:
Code: Select all
<?PHP
session_start();
if (!session_is_registered("super")) {
Sorry you not registered :^(
}
User avatar
WiZARD
Moderator
Moderator
 
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol

Re: Session missing in passing thru pages

Postby ransome » Thu Jan 16, 2003 5:09 pm

WiZARD wrote:At each page you do:
Code: Select all
<?PHP
session_start();
if (!session_is_registered("super")) {
Sorry you not registered :^(
}


Dear WiZARD,

How can I do after checking whether the session is registered or not :

say not:

Do I code that :

if (!session_is_registered("super"))
{
global $super;
$super = $username;
session_register("super");
}


Will the sessionID generated is the same as the sessionID I registered before ?




With regards,


Ransome
ransome
New php-forum User
New php-forum User
 
Posts: 7
Joined: Tue Jan 14, 2003 6:36 pm

Re: Session missing in passing thru pages

Postby WiZARD » Fri Jan 17, 2003 12:56 am

How about this?
Code: Select all
session_start();

function GetSession($parameter_name)
{
    global $HTTP_SESSION_VARS;
    return isset($HTTP_SESSION_VARS[$parameter_name]) ? $HTTP_SESSION_VARS[$parameter_name] : "";
}

function SetSession($param_name, $param_value)
{
  global ${$param_name};
  if(session_is_registered($param_name))
    session_unregister($param_name);
  ${$param_name} = $param_value;
  session_register($param_name);
}
if (!GetSession("User")) {
SetSession("User", "super");
SetSession("Rights", "none");
}


From worked project.....
User avatar
WiZARD
Moderator
Moderator
 
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol

Postby Redcircle » Tue Jan 21, 2003 11:26 pm

using the $_SESSION array would be an easier to understand and less complex way to do it.

Translated code.
Code: Select all
session_start();

function GetSession($parameter_name)
{

    return isset($_SESSION[$parameter_name]) ? $_SESSION[$parameter_name] : "";
}

function SetSession($param_name, $param_value)
{
 if(!isset($_SESSION[$param_name]))
    $_SESSION[$param_name] = $param_value;
}
if (!GetSession("User")) {
SetSession("User", "super");
SetSession("Rights", "none");
}



notice the SetSession is much more readable. and since the $_SESSION is a global array it will act just like other arrays and you will not have to make a global command which is something I always forget to do. Just one note. in order to use this method you must be using PHP 4.1 or later.
User avatar
Redcircle
Moderator
Moderator
 
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA

Postby WiZARD » Wed Jan 22, 2003 8:03 am

I know what this code work under PHP 4.1.x and later...
P.S. Some program may be fat but work faster......
Some instructions of PHP work faster but if you programming this, you need more typing code :wink: :?
User avatar
WiZARD
Moderator
Moderator
 
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol


Return to PHP General

Who is online

Users browsing this forum: Baidu [Spider] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.