I have following code in the start of my php file but somehow it doesnt redirect to login page, however if I separate if condition into sub if condition then redirection works
Below code doesnt work
if (!isset($_SESSION['emailid'], $_SESSION['roleid']) && $_SESSION['roleid'] != 1) {
header('location:login.php?lmsg=true');
exit;
}
///This doesnt work
Below code work
if (!isset($_SESSION['emailid'], $_SESSION['roleid'])) {
header('location:login.php?lmsg=true');
exit();
}
if ($_SESSION['roleid'] != 1) {
header('location:login.php?lmsg=true');
exit();
}
///this works
can someone help?
This is easier to read and it's probably want you meant to do:
if (!isset($_SESSION['emailid'], $_SESSION['roleid']) or (isset($_SESSION['roleid']) and $_SESSION['roleid'] != 1)) {
header('location:login.php?lmsg=true');
exit;
}
Related
This one may take a little but please stay with me for it.
I have been working with this code:
if(isset($_GET['o']) && isset($_GET['q'])){
echo("WIN 1");
if($_GET['o'] == "remove") {
echo("WIN 2");
$_SESSION['product_'.$_GET['q']]-=1;
echo("WIN 3");
if($_SESSION['product_'.$_GET['q']] < 1) {
unset($_SESSION['product_'.$_GET['q']]);
echo("WIN 4");
redirect("checkout.php");
} else {
echo("LOSE");
redirect("checkout.php");
}
}
}
And it is not performing any of the functions unless I step through it using:
exit();
After each of the echos.
I have no idea what could be causing this issue.
The URL being passed is:
localhost/cart.php?q=1&o=remove
Thank you!
EDIT
This is how I set the session (I have removed the echos):
if(isset($_GET['q'])) {
$query = query("SELECT * FROM products WHERE product_id=".escape_string($_GET['q'])."");
confirm($query);
while($row = fetch_array($query)){
if($row['product_quantity'] != $_SESSION['product_'.$_GET['q']]){
$_SESSION['product_'.$_GET['q']]+=1;
redirect("checkout.php");
} else if($row['product_quantity_tracking'] == "No") { //CHECK TO SEE IF IT'S INVENTORY TRACKED
redirect("checkout.php");
} else {
set_message("Sorry! There isn't enough in stock to complete that request!");
redirect("checkout.php");
}
}
}
This is the redirect function:
function redirect($location){
header("Location: $location");
}
I hope this helps!
By using exit(); you stop the execution of your script and you can see everything that was printed out using your echos.
-> If you can the see something like 'WIN 1', 'WIN 2', ... when exiting your script, it is working fine until this line.
-> Your problem must be found after this line.
I guess your problem lies in the redirect() function.
See this for how to perform a redirect: How do I make a redirect in PHP?
So, for some reason, when I hit the redirect on the cart.php it was running the REST OF THE PAGE instead of just redirecting and exiting the page as it would be expected to do.
For me to have solved this issue, I had to change the code to the following:
if(isset($_GET['o']) && isset($_GET['q'])){
if($_GET['o'] == "remove") {
$_SESSION['product_'.$_GET['q']]-=1;
if($_SESSION['product_'.$_GET['q']] < 1) {
unset($_SESSION['product_'.$_GET['q']]);
redirect("checkout.php");
exit();
} else {
redirect("checkout.php");
exit();
}
}
}
By adding the exit(); after the redirects it made sure it didn't follow the rest of the script on the page.
Thank you for everyone's help!
I've looked at several of the "PHP redirect not working ......" posts, but I haven't had any luck figuring out what is going on with my problem.
The code below always goes to teacherActivity_2.php even though the 2nd if passes and should redirect to teacherActivity_3_shadowT.php. If I comment out the teacherActivity_2.php, the page redirects (as expected) to teacherActivity_3_shadowT.php. I think I'm missing some obscure rule that I should know but don't. Any ideas?
Why does this redirect to teacherActivity_2.php instead of teacherActivity_3_shadowT.php?
if($test == 'yes') {
if($_SESSION['wblReporting']['activity'] == 'shadowT'){
header('Location: /app/do_cte-wbl/forms/teacherActivity_3_shadowT.php');
}
header('Location: /app/do_cte-wbl/forms/teacherActivity_2.php');
}
?>
Try the following:
if($test == 'yes') {
if($_SESSION['wblReporting']['activity'] == 'shadowT'){
header('Location: /app/do_cte-wbl/forms/teacherActivity_3_shadowT.php');
die();
} else {
header('Location: /app/do_cte-wbl/forms/teacherActivity_2.php');
die();
}
}
This worked no matter how many elseifs I used.
When a user opens a php page, can I make the page to reload by itself for two times before showing the contents of it to the user?
I tried to use:
header("Location: http://url");
but it goes on loop and never loads the page.
This is veeeery unusual, what I could think of is:
URL: page.php
if (!isset($_GET["time"]) && !isset($_GET["done"]))
{
header("Location: http://url.com/page.php?time=1");
exit;
}
else if ($_GET["time"] == 1)
{
header("Location: http://url.com/page.php?time=2");
exit;
}
else if ($_GET["time"] == 2)
{
header("Location: http://url.com/page.php?done=1");
exit;
}
Or you could use sessions, but good luck with that.
Total noob to PHP and I am trying to put this in the header of my wordpress file:
<?php
$meta = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
$country = $meta['geoplugin_countryCode'];
if (($country=='GB') or ($country=='US')) {
exit;
} else {
header("Location: http://google.com");
exit;
}
?>
Where the page code would get executed if from GB or US else get kicked to google.
Can't get it to work so guidance required please.
Cheerz
Ukphoneguy
You don't want to exit; in the "allowed" case.
$allowed = array('GB', 'US');
if (!in_array($country, $allowed)) {
// Not an allowed country. Redirect.
header("Location: http://google.com");
exit;
}
// The rest of your code for valid countries goes here
Your problems is that you were calling exit, even for GB and US. You only call exit when you want to completely stop executing any more PHP code (which is not what you wanted).
I would suggest:
if (($country != 'GB') and ($country != 'US')) header("Location: http://google.com");
This is my code for userslist.php. I put it above the head of this page so if this link is clicked, only admin can enter the page as filtered that is why I have redirections.
session_start();
$loggedInfo['username'] = $_SESSION['username'];
if(
isset($loggedInfo['username']) && $loggedInfo['username']==="admin" &&
trim($loggedInfo['username']) != "guest"
)
{
header('Location: userslist.php');
}
else {
header('Location: ../index.php');
}
This is my php script and I got a problem with redirecting. On the header(location ...) when I changed it to echo true or false, the echo returns the value correctly. But when I put a redirect/location, it does say:
This webpage has a redirect loop
Why is that? :(
Put this code in top of the userlist.php.An try what you got
<?php session_start();
$loggedInfo['username'] = $_SESSION['username'];
if(isset($loggedInfo['username']) && $loggedInfo['username']!="admin"){
header('Location: ../index.php');
exit();
}else if(isset($loggedInfo['username']) && $loggedInfo['username']=="admin"){
?>
You page code here goes
<?php } ?>
You're probably including this code in all pages. Thus on userslist.php it will also redirect to userslist.php. This causes permanent redirects, which is a redirect loop.
This conclusion is however difficult to support without seeing all the code you are using.