Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need a little help with a url variable 1

Status
Not open for further replies.

jefargrafx

Instructor
Joined
May 24, 2001
Messages
273
Location
US
I got one file that includes another (a navigation panel).

I'd that php to echo a link based on a url variable.
here's my code
<?php
if ($HTTP_GET_VARS['affiliate'] == "wilsons") {
echo "";
} else {
echo "Loyal To The Core<br>";
}
?>

word fine in the parent file when I include one file for the other, but that requires me to manager two naviagtion files.

I'd rather use only one and change the links based on the url variable file.php?panel=jef


cann't an incldued file read the variable from it's parent?

if not how would yall do this with one file

let me know

jef
 
Hi Jef,

I don't *think* that include file have access to parent's $HTTP_GET_VARS -however, I'm not sure about this at all....

Try this in your include file instead (should work ....):

Code:
<?php 
  $affiliate = $_REQUEST["affiliate"];
  if ($affiliate == "wilsons") {
      echo ""; 
  }    else {
   echo "Loyal To The Core<br>"; 
  }
?>

Good Luck §;O)


Jakob

 
Don't use $_REQUEST. It has the same variable poisoning problems you get by having register_globals turned on.

Use $_GET instead.


An included file inherits the variable scope of its parent script. Unless you are inside a function, $HTTP_GET_VARS should be available.

But if you are running PHP 4.1.0 or newer, use $_GET instead. $_GET is not just global but superglobals, so it's available inside functions, too. Also, the default for PHP 5.0 is that the "long" variable names ($HTTP_GET_VARS, $HTTP_POST_VARS, etc) will not be instantiated, so it's a good idea to get into the habit of using the "short" superglobals.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
no dice, gave it a try and dummer,

but ya got my mind thinking,

anyone solve this already?

jef
 
Here's my test code:

test_inherit.html:
Code:
<html>
	<body>
		<form method="get" action="test_inherit1.php">
			<input type="text" name="affiliate"><br>
			<input type="submit">
		</form>
	</body>
</html>

test_inherit1.php:
Code:
<?php
print 'In main file: $_GET[\'affiliate\'] = "' . $_GET['affiliate'] . '"<br>';
print 'In main file: $HTTP_GET_VARS[\'affiliate\'] = "' . $HTTP_GET_VARS['affiliate'] . '"<br>';

function foo()
{
	print 'In main file in function: $_GET[\'affiliate\'] = "' . $_GET['affiliate']. '"<BR>';
	print 'In main	 file in function: $HTTP_GET_VARS[\'affiliate\'] = "' . $HTTP_GET_VARS['affiliate']. '"<BR>';
}

foo();

include ('test_inherit2.php');

?>

test_inherit2.php:
Code:
<?php
print 'In included file: $_GET[\'affiliate\'] = "' . $_GET['affiliate']. '"<BR>';
print 'In included file: $HTTP_GET_VARS[\'affiliate\'] = "' . $HTTP_GET_VARS['affiliate']. '"<BR>';

function bar()
{
	print 'In included file in function: $_GET[\'affiliate\'] = "' . $_GET['affiliate']. '"<BR>';
	print 'In included file in function: $HTTP_GET_VARS[\'affiliate\'] = "' . $HTTP_GET_VARS['affiliate']. '"<BR>';
}

bar();	
?>

When I point my browser at test_inherit.html, enter "snafu" into the form and submit the form, the script returns:

[tt]In main file: $_GET['affiliate'] = "snafu"
In main file: $HTTP_GET_VARS['affiliate'] = "snafu"
In main file in function: $_GET['affiliate'] = "snafu"
In main file in function: $HTTP_GET_VARS['affiliate'] = ""
In included file: $_GET['affiliate'] = "snafu"
In included file: $HTTP_GET_VARS['affiliate'] = "snafu"
In included file in function: $_GET['affiliate'] = "snafu"
In included file in function: $HTTP_GET_VARS['affiliate'] = ""[/tt]


This behavior is exactly as described in the PHP online manual section titled "Variable Scope"



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
that's the trick?

outstanding very helpful

a little more to it than I thought but I think I can use this.

thanks


jef
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top