View Full Version : global variables?
duniyadnd
12-27-2002, 04:51 AM
I'm not sure how this works that well.
I've got page A and page B, where page A has this code that includes page B.
page A:
<html>
<body>
<? include 'pageB.php'; ?>
</body>
</html>
page A always has two attributes on the URL (lets say cond1 and cond2).
So, we have domain/PageA.php?cond1=xyz&cond2=zyx
Now if I call these conditions and compare them with the sql database that I have, it all works fine from Page A. However, I can't seem to get any information off the URL using Page B. Any way of going around this?
ie. Page A would get the two conditions onto the variables, but Page B would get nothing literally.
Thanks
Duniyadnd
mordred
12-27-2002, 02:50 PM
Included files have access to all variables available in the including file (so to say the parent). How exactly are you retrieving the GET parameters? Because this in pageB.php
var_dump($_GET);
works pretty well for me and returns the desired output. Maybe something in your query is wrong and thus the error is somewhere else?
duniyadnd
12-27-2002, 11:24 PM
I tried var_dump() function and it returned an empty array from page B, but the array was filled up on Page A.
How would I call it from Page B if that were the case? I figure it doesn't get the URL at all.
Duniyadnd
The variables in the query string are not global, as such. They're part of the $_GET variables and available from the page you are on.
If they're accessible from one part of a page, they're accessible from another. Including does not actually instantiate another page, it simply adds the code to an existing page (as far as the parser is concerned, it treats the code as if it's all on one page). When you're using include, page a and page b are the same set of code, with access to the same variables.
There must be something in one of these two pages that's unsetting the $_GET array. Without knowing anything about what's on that page, it's impossible to say what that is. My suspiscion is that a new page header is being sent at some point.
A simple solution is to put the functionality for page b into a function (which is should be anyway), include page b earlier in the page, and then call it from a function within the html.
duniyadnd
12-28-2002, 01:42 AM
i was probably not clear... here's the code:
Page B:
<? echo 'cID: '. $cID.'<br />'; ?>
Page A:
<?
$URL = "http://www.mydomainname.com/";
$cID = $HTTP_GET_VARS['cID'];
//global $cID;
include 'pageB.php';
?>
Page B doesn't read $cID at all. As you can see in page A, i even tried to make the variable in PageA global.
Duniyadnd
It works for me. I enter the following query string:
http://localhost/test/pageA.php?cID=1234
The two pages are as you showed: pageA.php:<?
$URL = "http://www.mydomainname.com/";
$cID = $HTTP_GET_VARS['cID'];
include 'pageB.php';
?>And pageB.php<? echo 'cID: '. $cID.'<br />'; ?>And the outputted html:cID: 1234<br />All I can suggest is confirming that pageB is doing anything -- ie do you get the static amounts copied? Other than that, the problem doesn't appear to be with the code you posted, but with some other settings.
duniyadnd
12-28-2002, 06:36 AM
Yeah, that's what i thought.
Anyway, i found out what the problem was, but don't know why it was a problem in the first place so if someone can explain the mechanics of why it was messed up in the first place, would greatly appreciate it.
I had a constant variable of my domainname, and called it $URL.
So, for all my includes, which worked fine when I didn't have to transfer variables across, i used:
include ''.$URL.'directory/name_of_file.php';
That was the problem, when I removed $URL and coded just the location, it worked.
Any thoughts? As I thought it should have mattered, unless there's something amiss about the DNS and its location each time etc.
Duniyadnd
duniyadnd
12-28-2002, 12:56 PM
Nevermind, didn't think of domainnames as DNS. Neeeeeeed sleeeeeep.... :rolleyes:
Here's an excerpt from an contributed statement in php.net
Note also that the URL shown in $HTTP_REFERER is not always the URL of the web page where the user clicked to invoke the PHP script.
This may instead be a document of your own web site, which contains an HTML element whose one attribute references the script. Note also that the current page fragment (#anchor) may be transmitted or not with the URL, depending on the browser.
Examples:
<FRAME src="your-page-script.php"8>
<IMAGE src="your-image-script.php">
In such case, browsers should transmit the URL of the container document, but some still persist in using the previous document in the browser history, and this could cause a different $HTTP_REFERER value be sent when the user comes back to the document referencing your script. If you wanna be sure that the actual current document or previous document in the history is sent, use client-side JavaScript to send it to your script:
<SCRIPT language="JavaScript"><!--
document.writeln('<FRAME src="your-page-script.php?js=1&ref=' +
document.location + '">');
--></SCRIPT><NOSCRIPT>
<FRAME src="your-page-script.php?js=0">
</NOSCRIPT>
And then check the value of $js in your page script to generate appropriate content when the remote user agent does not support client-side scripts (such as most index/scan robots, some old or special simplified browsers, or browsers with JavaScript disabled by their users).
Duniyadnd
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.