Jump to content

Recommended Posts

I have searched through the forums and have yet to see an answer. On my website I pass variables from one script to another using links like "blah.php?p=1&c=1&v=2". This works very well but I'm concerned that it won't in future as it requires "register_globals=on" to work. My host has it turned "off" (quite rightly) and I have it "on" for my site (use a php.ini snippet in the root of my site to do this).

 

I see a fair amount of reference to "$_GET" with suggestions to use it in place of the "register_globals" settings. I need to pass the values to the new script so that it displays the correct menus. I also use it to control which image is displayed in some galleries I have and to control the forward and back buttons within these galleries. Example of the code:

 

This is the calling link:

 

<a href="tvl_image.php?p=2&c=3&v=2"><img src="Images/tvl/tvl3_tn.jpg" title="Old Customs House"></a>

 

And this is the first few lines of the script:

 

if (!$p) { $p=2; }
if (!$v) { $v=1; }
if (!$c) { $c=1; }
//
// Test for the variables and sets defaults if not found.
// Otherwise uses the passed values.

 

The script then continues on with all the variables passed. I am no expert when it comes to PHP but have written the entire website in PHP. How do I change the way this all works without using "register_globals" and, at the same time, keeping the code simple?

Link to comment
https://forums.phpfreaks.com/topic/127656-passing-values/
Share on other sites

To make your code work with register_globals disabled, change the code snippet to

<?php
$p = (isset($_GET['p']))?$_GET['p']:2;
$v = (isset($_GET['v']))?$_GET['v']:1;
$c = (isset($_GET['c']))?$_GET['c']:1;
?>

 

Translated these lines say to set the variable $p, $v, or $c to the corresponding index in the $_GET superglobal array if that index exists, if it doesn't exist set the variable to the default.

 

Register_globals has been turned off as the default since version 4.2 -- over 4 years ago. You REALLY should learn how to write your scripts without relying on it.

 

To Maq: using empty() here won't work correctly if the value on the URL is 0 (zero), since that will be cause empty() to return true, which is false...

 

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/127656-passing-values/#findComment-660623
Share on other sites

Thank you both (Maq and Ken). You have answered my question perfectly. I am in the process of changing and testing the code (found a couple of logic problems but have that under control). One more question, if I may:

 

My "index.php" file contains an "include()" to load the title of the page and the nav bar. With "register_globals=On" I simply called it like this:

 

<? 
$p=1;
include("title.php");
?>

 

and it worked correctly. Now, using the "GET" method the variable ($p) is not being passed to the "title.php" script. I am working on ways around it but would like to know why this now doesn't work as it should? The code snippet that it applies to is as in my original post above. I tried calling it with "include("title.php?p=1")" but that generates an error.  :(

Link to comment
https://forums.phpfreaks.com/topic/127656-passing-values/#findComment-660779
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.