0

I have a webpage which holds the number of the page to display:

mydomain.net/index.php?page=42

This works alright. Now I want to display the page only when a particular cookie is set, and I moved most of the body to an include file, so that index.php only has

<?php 
    if ($cookie_ok):
        include("http://mydomain.net/index_d6skrif9.php"); 
    else:
        include("http://mydomain.net/noaccess.inc"); 
    endif 
?>

and now the $_GET["page"] in the include file, which is supposed to retrieve the page number returns nothing.

I read that $_GET[] is a superglobal and that superglobals' scopes are across include files. So what's wrong here, and how can I use the page number in the include file?

1
  • 2
    You're not including another PHP file. You are retrieving the HTML output over your webserver. And this basically remote PHP process won't see the local superglobals.
    – mario
    May 28, 2014 at 13:39

2 Answers 2

3

$_GET works in included files, it does not work in included HTTP resources.

The PHP in index_d6skrif9.php will be executed by mydomain.net before it gets to the PHP program with the include statement in it.

Use a local file path, not an HTTP URL.

include("index_d6skrif9.php"); 

Alternatively, pass the value to the server you are pulling the include from:

include("http://mydomain.net/index_d6skrif9.php?page=" + urlencode($_GET['page'])); 

Note that the latter approach has far more opportunity for things to go wrong and is much less efficient than a local file included, so it isn't recommended if you can help it.

0

You'll need to include them locally, not over-the-web:

if ($cookie_ok) 
    include("index_d6skrif9.php"); 
else
    include("noaccess.inc"); 
endif 

By using URLs, you're making a web-request and the server executes the PHP in the file and returns the contents (making it have its own set of super-globals).

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.