Wrox Programmer Forums
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 21st, 2003, 09:08 PM
Authorized User
 
Join Date: Jun 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to natmaster Send a message via AIM to natmaster Send a message via MSN to natmaster
Default $GLOBALS

i was trying to make a script that would - if the user tried to access one of my pages without my navigation frames around it, it would redirect them to the frameset page and then the main frame would load from the page they were trying to view.


what i have:

javascript on every content page (this works):
if (top == self) self.location.href = "../assets/make-context.php";

make-context.php:
<?php
$GLOBALS['page']=$_SERVER['HTTP_REFERER'];
header("Location:../index.php");
?>

frameset page (main frame part):
<frame name="main" src="<?php if(isset($GLOBALS[page])) {echo $GLOBALS[page]; unset($GLOBALS[page]);} else echo "main.php";?>">


this would work fine if $GLOBALS[page] existed after i assigned it a value. i tried echoing it's value the line after but it had no value. is there some kind of configuration problem with php? the php website said this global variable exists. what am i doing wrong?

----------------------------
http://aeonofdarkness.com
__________________
----------------------------
Aeon of Darkness MUD - Free Online Roleplaying Game
http://aeonofdarkness.com
 
Old July 21st, 2003, 11:27 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Since $_SERVER["HTTP_REFERER"] does not always have a value, your error could be in the assigning it to the$GLOBALS["page"] variable. This variable only has a value if there is a referer. This is my suggestion:

Personally I would add a $_GET parameter to any page I was redirecting with the value of $_SERVER["PHP_SELF"] as its value, so as to not rely on the $_SERVER["HTTP_REFERER"] which may or may not have a value and if it does have a value it may not be from the source that you want it to be, e.g. a search engine... this value can also be altered in some browsers so it is not to be trusted.

//javascript

if (top == self) self.location.href = "../assets/make-context.php?referring_page=<?php echo $_SERVER["PHP_SELF"]; ?>";

frameset page (main frame part):

if ($_SERVER["PHP_SELF"] == "/make-context.php") {

     if (isset($_GET["referring_page"])) {

        header("Location:../index.php?frame_value={$GET["referring_page"]}");

     } else {

        //default

     }

}


<frame name="main" src="<?php

if(isset($_GET["referring_page"])) {

      echo $_GET["referring_page"];
      // unset not necessary.

} else {

   echo "main.php";

}

?>">

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old July 21st, 2003, 11:30 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Oops, I left out an underscore in this $_GET reference...

header("Location:../index.php?frame_value={$_GET["referring_page"]}");

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old July 21st, 2003, 11:45 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Oh good Lord!

I screwed up again...

This reference is supposed to be '$_GET["frame_value"]'... I'm sure you get the idea!

if(isset($_GET["frame_value"]{

      echo $_GET["frame_value"];
      // unset not necessary.

} else {

   echo "main.php";

}

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old July 22nd, 2003, 01:25 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

All your replies are forgetting one fundamental thing...

Natmaster, you set $_GLOBALS['page'] immediately before redirecting to a new page. What makes you think that your old variables are still available in a new page request?

Also -- you keep forgetting to make your array indexes strings. It's not $_GLOBALS[page], it's $_GLOBALS["page"] or $_GLOBALS['page']. An unquoted single token is first assumed to be a defined constant. If it's not a named constant, PHP issues a warning and then treats the token as a string.

The problem is, of course, that your script would fail and cause errors when running with error_reporting = E_ALL.


Take care,

Nik
http://www.bigaction.org/
 
Old July 22nd, 2003, 02:04 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Ah,
I see now! I missed it. So sorry.

For some reason I completely blacked out the reference to 'header' when I was explaining why your script didn't work. Hey that's alright though, I'm entitled to all the stupid attacks I need to get through the day! At least you can see a working example of what its supposed to do! My point was indeed that you had to pass the value with the $_GET method or another method allowing persistence of your data.

Perhaps you were getting $GLOBAL confused with $_SESSION. No definitely not persistent between HTTP requests.

Nik -- You're like a human calculator man! How do you keep all this knowledge straight? My mind is definitely not as organized!

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old July 22nd, 2003, 02:15 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It's nothing, really. Natmaster said that some variable didn't exist, so the first thing you should do in that situation is trace through the script's execution to see where (or, as in this case, IF) a variable is set.

You're correct in suggesting that he use $_SESSION to store persistent data -- that's what sessions are for, after all!



Take care,

Nik
http://www.bigaction.org/
 
Old July 23rd, 2003, 04:27 PM
Authorized User
 
Join Date: Jun 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to natmaster Send a message via AIM to natmaster Send a message via MSN to natmaster
Default

then what DOES $GLOBAL do? it was on this page: http://www.php.net/reserved.variables

also, nik, you refered to it with an underscore...but that page had it without an underscore...i'm so confused

----------------------------
http://aeonofdarkness.com
 
Old July 23rd, 2003, 04:32 PM
Authorized User
 
Join Date: Jun 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to natmaster Send a message via AIM to natmaster Send a message via MSN to natmaster
Default

ques, you had:
if (top == self) self.location.href = "../assets/make-context.php?referring_page=<?php echo $_SERVER["PHP_SELF"]; ?>";

i'm not sure that would work given the javascript is in a .js file that is included in some pages that are not .php

----------------------------
http://aeonofdarkness.com
 
Old July 23rd, 2003, 05:28 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

$GLOBALS takes all variables defined in global scope within a script and makes them accessible within any scope.

$GLOBALS is a superglobal array,
meaning that if you define a variable in the regular scope of a script:

$foo = "hello mom!";

And want to access that variable within a function,
instead of doing the global keyword:

global $foo;

or passing it to a function

$some_return_data = foo_function($foo);

You can access it directly within the function with the $GLOBALS array, thereby by-passing the other methods

function foo_function($some_parameters) {

 //this will reference the $foo variable originally set
 echo $GLOBALS["foo"];

 //this will output nothing
 echo $foo;

 //this will output the $foo variable as passes as a parameter
 echo $some_parameters;

}

But by Global, this variable does not imply persistence of data between HTTP requests, for that you will need $_SESSION.

hth,
: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in Globals class pedroabs BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 8 June 12th, 2008 01:11 PM
Globals nabeelalkaff BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 April 29th, 2007 08:26 PM
Chapter 3 code available w/globals off sambrose BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 5 November 20th, 2006 05:34 AM
register globals problem nulogix Beginning PHP 4 June 16th, 2004 08:49 AM
Register Globals Off cmiller Beginning PHP 4 August 18th, 2003 05:21 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.