Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Computing > NZ Computing > Quick PHP question about CGI environment variables

Reply
Thread Tools

Quick PHP question about CGI environment variables

 
 
Adam Cameron
Guest
Posts: n/a
 
      09-15-2004
G'day.
If I do this in ColdFusion:

structKeyExists(cgi, 'http_referer'): [#structKeyExists(cgi,
'oompahloompah')#]<br />
structKeyExists(cgi, 'oompahloompah'): [#structKeyExists(cgi,
'oompahloompah')#]<br />

Or this, in ASP:
Request.ServerVariables("http_referer") : [<% Response.Write
Request.ServerVariables("http_referer") %>]<br />
Request.ServerVariables("oompahloompah") : [<% Response.Write
Request.ServerVariables("oompahloompah") %>]<br />

I get empty strings returned for both (or the correct referer, if there is
one, obviously

Now, I'd be keen for some kind person to tell me what they get if one does
the equivalent thing in PHP? (and if anyone else who does other web-type
languages out there wants to put their oar in, that'd be grand, too

I have to say I'm no so concerned about the 'oompahloompah' side of things,
just that I would expect the referer variable to *not exist* if there
wasn't a referer value passed back from the HTTP server, not for it to be
an empty string. I can see arguments both ways, and I'm not that fussed,
I'm just interested in how other systems work.

Cheers.

Adam
 
Reply With Quote
 
 
 
 
Harry..
Guest
Posts: n/a
 
      09-15-2004
Adam Cameron wrote:

> G'day.
> If I do this in ColdFusion:
>
> structKeyExists(cgi, 'http_referer'): [#structKeyExists(cgi,
> 'oompahloompah')#]<br />
> structKeyExists(cgi, 'oompahloompah'): [#structKeyExists(cgi,
> 'oompahloompah')#]<br />
>
> Or this, in ASP:
> Request.ServerVariables("http_referer") : [<% Response.Write
> Request.ServerVariables("http_referer") %>]<br />
> Request.ServerVariables("oompahloompah") : [<% Response.Write
> Request.ServerVariables("oompahloompah") %>]<br />
>
> I get empty strings returned for both (or the correct referer, if there is
> one, obviously
>
> Now, I'd be keen for some kind person to tell me what they get if one does
> the equivalent thing in PHP? (and if anyone else who does other web-type
> languages out there wants to put their oar in, that'd be grand, too
>
> I have to say I'm no so concerned about the 'oompahloompah' side of
> things, just that I would expect the referer variable to *not exist* if
> there wasn't a referer value passed back from the HTTP server, not for it
> to be
> an empty string. I can see arguments both ways, and I'm not that fussed,
> I'm just interested in how other systems work.
>


Apparently in PHP you just declare the usual cgi environment variables
as global, and as with all environment variables a non-existent variable
will be null as distinct from an empty string. But you might need to
test that for yourself - might depend on your platform etc.

PHP also has session.referer_check which will also make the distinction
that you want.

 
Reply With Quote
 
 
 
 
Patrick Dunford
Guest
Posts: n/a
 
      09-15-2004
In article <1bvc91gehzpgq$.> in nz.comp on
Thu, 16 Sep 2004 10:49:20 +1200, Adam Cameron <>
says...
> G'day.
> If I do this in ColdFusion:
>
> structKeyExists(cgi, 'http_referer'): [#structKeyExists(cgi,
> 'oompahloompah')#]<br />
> structKeyExists(cgi, 'oompahloompah'): [#structKeyExists(cgi,
> 'oompahloompah')#]<br />
>
> Or this, in ASP:
> Request.ServerVariables("http_referer") : [<% Response.Write
> Request.ServerVariables("http_referer") %>]<br />
> Request.ServerVariables("oompahloompah") : [<% Response.Write
> Request.ServerVariables("oompahloompah") %>]<br />
>
> I get empty strings returned for both (or the correct referer, if there is
> one, obviously
>
> Now, I'd be keen for some kind person to tell me what they get if one does
> the equivalent thing in PHP? (and if anyone else who does other web-type
> languages out there wants to put their oar in, that'd be grand, too


It is still possible for that string to be cleared if there is no
referer. It does work in PHP though as I have tried it.

>
> I have to say I'm no so concerned about the 'oompahloompah' side of things,
> just that I would expect the referer variable to *not exist* if there
> wasn't a referer value passed back from the HTTP server, not for it to be
> an empty string. I can see arguments both ways, and I'm not that fussed,
> I'm just interested in how other systems work.


It depends on how your language of choice detects an empty variable,
among other things

If I reference $HTTP_REFERER in php I would expect to get an empty string
even if there wasn't one, because of the way PHP works.
 
Reply With Quote
 
Patrick Dunford
Guest
Posts: n/a
 
      09-15-2004
In article <a642d.2917$> in nz.comp on Thu, 16
Sep 2004 11:19:34 +1200, Harry.. <harry..@on..the.net> says...
> Adam Cameron wrote:
>
> > G'day.
> > If I do this in ColdFusion:
> >
> > structKeyExists(cgi, 'http_referer'): [#structKeyExists(cgi,
> > 'oompahloompah')#]<br />
> > structKeyExists(cgi, 'oompahloompah'): [#structKeyExists(cgi,
> > 'oompahloompah')#]<br />
> >
> > Or this, in ASP:
> > Request.ServerVariables("http_referer") : [<% Response.Write
> > Request.ServerVariables("http_referer") %>]<br />
> > Request.ServerVariables("oompahloompah") : [<% Response.Write
> > Request.ServerVariables("oompahloompah") %>]<br />
> >
> > I get empty strings returned for both (or the correct referer, if there is
> > one, obviously
> >
> > Now, I'd be keen for some kind person to tell me what they get if one does
> > the equivalent thing in PHP? (and if anyone else who does other web-type
> > languages out there wants to put their oar in, that'd be grand, too
> >
> > I have to say I'm no so concerned about the 'oompahloompah' side of
> > things, just that I would expect the referer variable to *not exist* if
> > there wasn't a referer value passed back from the HTTP server, not for it
> > to be
> > an empty string. I can see arguments both ways, and I'm not that fussed,
> > I'm just interested in how other systems work.
> >

>
> Apparently in PHP you just declare the usual cgi environment variables
> as global, and as with all environment variables a non-existent variable
> will be null as distinct from an empty string. But you might need to
> test that for yourself - might depend on your platform etc.


I presume if you test for null it will show as null, but if you treat it
as a string it will look like an empty string, because of the automatic
type conversion
 
Reply With Quote
 
Adam Cameron
Guest
Posts: n/a
 
      09-15-2004
> If I reference $HTTP_REFERER in php I would expect to get an empty string
> even if there wasn't one, because of the way PHP works.


So PHP won't error if you reference an undefined variable? Like, for
example, if I tried to use $OOMPAHLOOMPAH without declaring or initialising
it? Surely not. Or are you saying $HTTP_REFERER is always defined (just
without a value)?

Adam
 
Reply With Quote
 
Patrick Dunford
Guest
Posts: n/a
 
      09-16-2004
In article <> in nz.comp on
Thu, 16 Sep 2004 11:51:38 +1200, Adam Cameron <>
says...
> > If I reference $HTTP_REFERER in php I would expect to get an empty string
> > even if there wasn't one, because of the way PHP works.

>
> So PHP won't error if you reference an undefined variable? Like, for
> example, if I tried to use $OOMPAHLOOMPAH without declaring or initialising
> it? Surely not. Or are you saying $HTTP_REFERER is always defined (just
> without a value)?


PHP does not require predeclaration of variable names. $HTTP_REFERER is
just a variable name. The first time you reference it, if it doesn't
already exist, PHP creates it. Obviously the server will set it up if
there is something to put in it.

isset tells you whether a variable exists or not. I tested it and it did
work in my test script.

How does http_referer get set anyway?
Here's my test scripts:

test0.php
<?php
header("Location: test1.php");
?>

test1.php
<?php
if (isset($HTTP_REFERER) == FALSE)
{
echo ("Referrer does not exist<br>");
}
if (is_null($HTTP_REFERER) == TRUE)
{
echo ("Referrer null<br>");
}
echo $HTTP_REFERER;
?>

doesn't set $HTTP_REFERER

But
test00.php
<?php
echo "<a href='test1.php'>test1.php</a>";
?>

does work; it outputs the full URL of the script.
 
Reply With Quote
 
Chris Hope
Guest
Posts: n/a
 
      09-16-2004
Patrick Dunford wrote:

> In article <> in nz.comp on
> Thu, 16 Sep 2004 11:51:38 +1200, Adam Cameron <>
> says...
>> > If I reference $HTTP_REFERER in php I would expect to get an empty
>> > string even if there wasn't one, because of the way PHP works.

>>
>> So PHP won't error if you reference an undefined variable? Like, for
>> example, if I tried to use $OOMPAHLOOMPAH without declaring or
>> initialising
>> it? Surely not. Or are you saying $HTTP_REFERER is always defined (just
>> without a value)?

>
> PHP does not require predeclaration of variable names. $HTTP_REFERER is
> just a variable name. The first time you reference it, if it doesn't
> already exist, PHP creates it. Obviously the server will set it up if
> there is something to put in it.


This isn't quite true, depending on your level of error reporting. Under a
more strict error reporting level than most web servers have set up by
default, you would get a warning message doing "print $foo;" if $foo had
not already been assigned a value. However doing "$foo = 'bar';" then
"print $foo;" would be ok.

A lot of people who develop with PHP don't realise this because these
warnings are usually supressed. I develop with the reporting level at 15
(E_ERROR | E_WARNING | E_PARSE | E_NOTICE) so it shows all these warnings
as well but have it at 7 (all those but without E_NOTICE) when in
production. See http://nz.php.net/error_reporting for more info.

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
 
Reply With Quote
 
Lawrence DčOliveiro
Guest
Posts: n/a
 
      09-16-2004
In article < >,
Patrick Dunford <> wrote:

>If I reference $HTTP_REFERER in php...


That should be $_SERVER["HTTP_REFERER"].
 
Reply With Quote
 
Patrick Dunford
Guest
Posts: n/a
 
      09-16-2004
In article <ldo-> in nz.comp on Fri,
17 Sep 2004 10:24:07 +1200, Lawrence DčOliveiro <ldo@geek-
central.gen.new_zealand> says...
> In article < >,
> Patrick Dunford <> wrote:
>
> >If I reference $HTTP_REFERER in php...

>
> That should be $_SERVER["HTTP_REFERER"].


Can be both. The second format refers to an array structure, which also
exists. I've verified that the first form works, but it may be version
dependent.

 
Reply With Quote
 
Lawrence DčOliveiro
Guest
Posts: n/a
 
      09-17-2004
In article < >,
Patrick Dunford <> wrote:

>In article <ldo-> in nz.comp on Fri,
>17 Sep 2004 10:24:07 +1200, Lawrence D1Oliveiro <ldo@geek-
>central.gen.new zealand> says...
>> In article < >,
>> Patrick Dunford <> wrote:
>>
>> >If I reference $HTTP REFERER in php...

>>
>> That should be $ SERVER["HTTP REFERER"].

>
>Can be both.


The former version is deprecated, because autoregistered globals lead to
security risks. Set your php.ini to "register_globals = Off" today, and
fix your scripts!

Lawrence
also not a fan of magic_quotes_gpc, but will rant about that another day
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick CGI question (specific to the CGI package) Ted Byers Perl Misc 19 11-30-2009 04:31 AM
PHP Training Institute In Delhi, Live Projects on PHP. Short TermPHP Courses, PHP Scripts, PHP Training with Live Projects. Rajive Narain Java 0 09-18-2009 10:47 AM
access cgi environment variables bbxrider ASP General 1 05-31-2005 01:03 AM
Quick question, hopefully quick answer. ~misfit~ NZ Computing 114 01-06-2005 01:36 PM
Quick Question Quick Answer JKop C++ 11 05-24-2004 09:46 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57