Ubicación del encabezado PHP con clave de matriz y operaciones_permitidas
-
12-12-2019 - |
Pregunta
En mi iniciar sesión.php pagina tengo esto:
$allowed_operations = array('foo', 'lorem');
if(isset($_GET['p'])
&& in_array(isset($_GET['p']), $allowed_operations)){
switch($_GET['p']){
case 'foo':
// Code for 'foo' goes here
break;
case 'lorem':
// Code for 'lorem' goes here
break;
}
}
donde si llamo a la url http://ejemplo.com/login.php?p=foo la función foo se llama.
¿Es posible que pueda llamar a esta URL sin agregar un href? http://ejemplo.com?p=foo en mi marcado html?
Por ejemplo algo como esto:
<?php
if (array_key_exists("login", $_GET)) {
$p = $_GET['p'];
if ($p == 'foo') {
header("Location: login.php?p=foo"); // This doesn't work
// And if I remove the ?p=foo,
// it redirect to the page but
// the 'foo' function is not called
}
}
?>
y mi html:
<a href="?login&p=foo">Login Foo</a> <br />
Solución
esto se debe a los infinitos bucles de redireccionamiento de páginas.que será creado por su código.
$p = $_GET['p'];
if ($p == 'foo') {
header("Location: login.php?p=foo"); // This doesn't work
// And if I remove the ?p=foo,
// it redirect to the page but
// the 'foo' function is not called
}
}
cada vez que ejecute el código en esta página la condición se establecerá en verdadera, es decir $_GET['p']
siempre mantendrá el valor foo y redirigirá una y otra vez a la misma página.detectando qué PHP dejará de ejecutar su script.
No puedo entender por qué desea redirigir a la misma página nuevamente, incluso si se cumple la condición.Mi sugerencia es evitarlo.simplemente verifique si la variable quiere redirigir a la misma página en caso afirmativo.luego omita la página; si no, redirija al destino preferido.
if (array_key_exists("login", $_GET)) {
$p = $_GET['p'];
if ($p == 'foo') {
//sice the variable foo redirects to the same page skip this path and do nothing
} else {
//any other url redirection goes here
header('Location: index.php?bar');
}
}
aunque podría haber otra manera.El código anterior también debería funcionar y evitará entrar en un bucle infinito de redireccionamiento de páginas.
Otros consejos
No creo que esto sea correcto:
$allowed_operations = array('foo', 'lorem');
if(isset($_GET['p']) && in_array(isset($_GET['p']), $allowed_operations)){
debería ser
$allowed_operations = array('foo', 'lorem');
if(isset($_GET['p']) && in_array($_GET['p'], $allowed_operations)){
y deberías usar
<a href="login&p=foo">Login Foo</a> <br />
y este es un bucle sin fin
if (array_key_exists("login", $_GET)) {
$p = $_GET['p'];
if ($p == 'foo') {
header("Location: login.php?p=foo"); // This doesn't work
Hay un error aquí:
<a href="?login&p=foo">Login Foo</a> <br />
Correcto:
<a href="login.php?p=foo">Login Foo</a> <br />
Y además, el bucle es interminable.Cuando ingresas a login.php, pides ir una y otra vez...Cree una función de "descanso" después de la primera vez.