Jump to content

[SOLVED] Session Variables with Register Globals turned off


phantomlimb

Recommended Posts

Hi,

 

I have recently moved hosts. My new host has register globals turned off, while my previous one had register globals on.

 

This code used to work, i could pass the variable $ptag between two pages.

 

But i would like to rewrite it to be compatible with register globals off. This is the first time i have come across this problem, and i've done some searching but am unable to ascertain which part of the code is causing the problem. Any ideas on how i could rewrite this block of code to resolve this issue?

 

 

<?php
session_start();  
if ( !session_is_registered( 'ptag' ) ) 
{ 
session_register( 'ptag' );
}
//if ptag value has been set via form method post 
if (isset($_POST['ptag'])) {
    $ptag = implode(',', $_POST['ptag']);
}
//if reset value has been set via form method post set ptag to blank
if (isset($_POST['reset'])) {
$ptag = "";
}
?>

All references to $ptag must be changed to use $_SESSION['ptag'] and any use of session_is_registered() or session_register() must be corrected (depends on how they are being used.)

 

In general session_is_registered('ptag') would become isset($_SESSION['ptag']) and session_register('ptag') would become $_SESSION['ptag'] = $ptag; However, for the code you posted, the first if(){} statement is not needed ($_SESSION variables ARE already part of the session and don't specifically need to be 'registered') and the code would become -

 

<?php
session_start();  

//if ptag value has been set via form method post 
if (isset($_POST['ptag'])) {
    $_SESSION['ptag'] = implode(',', $_POST['ptag']);
}
//if reset value has been set via form method post set ptag to blank
if (isset($_POST['reset'])) {
$_SESSION['ptag'] = "";
}
?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.