kev1952 Posted October 9, 2008 Share Posted October 9, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/127656-passing-values/ Share on other sites More sharing options...
Maq Posted October 9, 2008 Share Posted October 9, 2008 Are you asking how to use the GET method? If so, do this with your example URL: $p = (empty($_GET['p'])) ? '2' : $_GET['p']; $v = (empty($_GET['v'])) ? '1' : $_GET['v']; $c = (empty($_GET['c'])) ? '1' : $_GET['c']; Quote Link to comment https://forums.phpfreaks.com/topic/127656-passing-values/#findComment-660622 Share on other sites More sharing options...
kenrbnsn Posted October 9, 2008 Share Posted October 9, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/127656-passing-values/#findComment-660623 Share on other sites More sharing options...
Maq Posted October 9, 2008 Share Posted October 9, 2008 @ken Would the empty function work the same as the isset in this case? Quote Link to comment https://forums.phpfreaks.com/topic/127656-passing-values/#findComment-660624 Share on other sites More sharing options...
kenrbnsn Posted October 9, 2008 Share Posted October 9, 2008 See my edited answer just above your question. Ken Quote Link to comment https://forums.phpfreaks.com/topic/127656-passing-values/#findComment-660625 Share on other sites More sharing options...
kev1952 Posted October 9, 2008 Author Share Posted October 9, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/127656-passing-values/#findComment-660779 Share on other sites More sharing options...
kenrbnsn Posted October 9, 2008 Share Posted October 9, 2008 Please post the contents of "title.php", then we can tell you how to fix the problem. Ken Quote Link to comment https://forums.phpfreaks.com/topic/127656-passing-values/#findComment-660838 Share on other sites More sharing options...
kev1952 Posted October 9, 2008 Author Share Posted October 9, 2008 Thanks for the quick response, Ken, but I've just solved it. There was an error in the code. Altered it and now works just fine. Thanks again for your help! Quote Link to comment https://forums.phpfreaks.com/topic/127656-passing-values/#findComment-660848 Share on other sites More sharing options...
Maq Posted October 9, 2008 Share Posted October 9, 2008 See my edited answer just above your question. I see, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/127656-passing-values/#findComment-660958 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.