Jump to content

Recommended Posts

I am haveing a few problems with session variables.

I can set them properly, and use them in scripts etc, however when I use a standard variabel with the same name as the session variable, it seems to override it.

For example:
[code]
$_SESSION['name'] = "value";
$name = "value2";
echo $_SESSION['name'];
[/code]

will print "value2".

Anyone know why?

Thanks in advance,
Alex
Link to comment
https://forums.phpfreaks.com/topic/32948-setting-session-variables/
Share on other sites

Thanks George,

I can assure you that with the hosting that I am using, it does not work correctly.

It does seem to work if that is the only code in the file, however in the following instance it does not (and it is not the only instance).

[code]

<?php
session_start();
include("includes/header.php");
include("login_functions.php");
include("includes/checklogin.php");

$admin_code = $_GET['admin_code'];
if ($admin_code == "admin_home" || $admin_code == NULL)
{


                   $_SESSION['test'] = "value1";
                   $test = "value2";
                   echo $_SESSION['test'];


      include("/dir/file.php");
} else {

include("file1.php");
include("file2.php");
include("file3.php");
include("file4.php");

}

include("navigation.php");
include("includes/footer.php");

?>

[/code]

The above code spits out "value2" on the echo line.

Alex.
Sounds like a register_globals problem. See the following from the php manual -
[quote]If register_globals is enabled, then the global variables and the $_SESSION entries will automatically reference the same values which were registered in the prior session instance. However, if the variable is registered by $_SESSION then the global variable is available since the next request. [/quote]
Yeah this sounds like a register_globals problem. When register global's is enabled it allows you to use the key to the super_global array as the name for the variable. For example you can use $name or $_SESSION['name'] to write/read to the session named name.

Regsiter globals is now set to off by default as of PHP4.2 as it can cause security exploits within your code. regsiter_globals will soon be gone for good as of PHP6 (thank god).

If your host allows you to use .htaccess files then I would recommend you add the following to a .htaccess file in your document root (the place where you upload your files to) with the following code in it:
[tt]php_flag register_globals off[/tt]

That should turn register_globals off throughout your website.

For more information on register globals please read [url=http://uk2.php.net/register_globals]this page[/url]
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.