In my login.php page I have this:
$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;
}
}
Where if I call the url http://example.com/login.php?p=foo the function foo is called.
Is it possible I can call this url without adding a href http://example.com?p=foo in my html markup?
For example something likes this:
<?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
}
}
?>
and my html:
<a href="?login&p=foo">Login Foo</a> <br />
this is because of the infinite page redirect loops. that will be created by your code.
every time you execute the code in this page the condition will be set to true, that is
$_GET['p']will always hold the value foo and it will redirect again and again to the same page. detecting which PHP will stop executing your script.I am unable to understand on why you would want to redirect to the same page again, even if the condition is met. my suggestion is to avoid it. simply check if the variable wants to redirect to the same page if yes. then skip the page if not then redirect to the preferred destination.
while there might be other way. the above code should also work, and will avoid getting into infinite page redirect loop.