Jump to content

Solving the issues with global variables turned off


jwmstudios

Recommended Posts

Ok after a few weeks of frustration i figured out how to get my variables to go back and forth pages without using register_globals = on

 

ok here is some test code:

 

First create a file called testpost.php

 

copy and paste this data:

 

<?php
  
  
   session_start();
      
  
    echo "<a href=\" get.php \" >Click here </a><br />"; 
   
    // looking for a returned value named Joe 
    if ($_SESSION['name'] == "Joe") {
       
       echo "Well What do you know i am".$_SESSION['name']."<br />";    // output on screen 
   }        
    
   // Well if i didn't find it, just call it mary. 
   $_SESSION['name']   = "mary";      
   
    
  
?>

 

fine then create  another file in the same directory called get.php 

copy and paste this into it:

 

<?php
   
   // Start Your Session each time save this file as get.php 
   session_start();

   $name = $_SESSION['name'];   // this is the original variable data that you want to store
  
   echo "my name is ".$name;  // output on screen  
  
   $_SESSION['name'] = "";   // resetting my session variable
  
   if($_SESSION['name'] == "") {
    
   $_SESSION['name'] = "Joe";  // reassign session variable to joe     
      
  }
  
  
  
  echo "<br /><a href=\"testpost.php\" >go back</a>";
  
  
  
?>

 

ok give it a test and then modify as you like, you don't have to send your variables across your web browser dudes, enjoy!

Firstly use unset() to unset your session. as doing $_SESSION['name'] = ""; may cause a false positive as its technically set with nothing. Or if thats the only session handle use session_destroy().

 

Secondly try something to the effect of

$session_start();
if(!isset($_SESSION['name'])){echo "I couldnt find the sessions, setting it now"; $_SESSION['name'] = "Mary";
elseif(empty($_SESSION['name'])){
echo "I found the session, but it contained only whitespace or was blank, setting it now"; $_SESSION['name'] = "Mary";
}elseif($_SESSION['name'] == "monkeytooth"){
echo "Hi <strong>".$_SESSION['name']."</strong> I don't like you! Go away";
session_destroy();
}else{
echo "Hi <strong>".$_SESSION['name']."</strong> how are you today?";
}

 

After the session is set you usually have to refresh your page to know it.

 

Ok i modified the get page using unset as suggested, it worked!

 

<?php
   
   // Start Your Session each time save this file as get.php 
   session_start();

   $name = $_SESSION['name'];   // this is the original variable data that you want to store
  
   echo "my name is ".$name;  // output on screen  
  
   unset($_SESSION['name']);   // resetting my session variable using unset function
  
   if(!isset($_SESSION['name'])) {
    
   $_SESSION['name'] = "Joe";  // reassign session variable to joe     
      
  }
  
  
  
  echo "<br /><a href=\"testpost.php\" >go back</a>";
  
  
  
?>

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.