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!

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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>";
  
  
  
?>

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.