$_GET[] no funciona en el archivo incluido?
Frecuentes
Visto 100 equipos
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
?>
y ahora el $_GET["page"]
in the include file, which is supposed to retrieve the page number returns nothing.
yo lei eso $_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?
2 Respuestas
3
$_GET
works in included archivos, it does not work in included HTTP resources.
The PHP in index_d6skrif9.php
será ejecutado por mydomain.net
before it gets to the PHP program with the include
declaración en él.
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.
contestado el 28 de mayo de 14 a las 14:05
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).
contestado el 28 de mayo de 14 a las 14:05
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas php scope superglobals or haz tu propia pregunta.
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