PHP not performing - php

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!

Related

PHP Redirect Not Working When In A Nested IF Statement

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.

php header location not redirecting page

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;
}

PHP Goto work around needed please

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");

Infinite redirect loop from PHP code in Wordpress

I have a PHP script which I will post below. It is a voting website, and my client only wants one user to be able to vote once based on their cookies and IP address.
After voting once, if the cookie or IP is detected as the same they are redirected to a fake voting pg which allows multiple votes. The browser loops between both the legal and duplicate vote pages.
Here is the code, I only added in the exit and die functions after getting this error and seeing online that might be the cause - however adding those functions made no difference.
$q = mysql_query("SELECT * FROM votelog");
while($row = mysql_fetch_array($q))
{
if(($ip = $_SERVER['REMOTE_ADDR']) == $row['ip'])
{
$duplicateIP = true;
}//end if
if(($row['pollid'] == 8))
{
$duplicatePoll = true;
}//end if
}//end while
//check cookies
if(isset($_COOKIE['poll']))
{
$cookieCheck = true;
}//end if
if((($duplicateIP == true) && ($duplicatePoll == true)) or ($cookieCheck == true))
{
show this pg
}//end if
else
{
echo '<meta http-equiv="refresh" content="0; url=/polls/legit" />'; //redirect to legal pg
exit();
die();
}//end else
Any ideas? The other page is the same except that the if and else are switched, like this:
if((($duplicateIP == true) && ($duplicatePoll == true)) or ($cookieCheck == true))
{
echo '<meta http-equiv="refresh" content="0; url=/polls/dupe" />'; //redirect to duplicate
exit();
die();
}//end if
else
{
show this pg
}//end else
P.S - I'm operating in a Wordpress environment
It is hard to guess whats going on there. But if your code is fine, i would expect that you have an caching issue.
Setting the headers (header()) correct, would help to solve that issue. But be carefull, you can make it more worse with setting wrong headers.
So an easy workaround could be to add an ?time() to your url. So the redirected URL would change each second.
echo '<meta http-equiv="refresh" content="0; url=/polls/dupe?'.time().'" />'; //redirect to duplicate
Just a side note:
exit();
die(); // this will never reached, as exit() and die() is the same
About exit() and die()

PHP Cannot redirect even if conditions are met

Here is my code
<?php
if (!isset($_SESSION)) { session_start(); }
if (!isset($_SESSION['username'])) { header("Location: index.php"); }
ob_start();
if($_POST) {
$id = $_POST['book_id'];
$command = $_POST['command'];
$sourcePage = $_POST['source'];
} else if ($_GET){
$command = $_GET['command'];
$sourcePage = $_GET['source'];
$id = $_GET['book_id'];
} else {
header("Location: index.php");
}
// if command is 2 then show cart content
if($command == 2) {
showCart();
// if command is 1 then add book to cart
} else if($command == 1) {
addToCart($id);
header("Location: $sourcePage");
// if command is 0, then remove book from cart
} else if($command == 0) {
deleteFromCart($id);
header("Location: $sourcePage");
} else if(!isset($command)){
header("Location: index.php");
}
ob_flush();
?>
Why is it that even if I'm not logged in, I'm not redirected?
is it possible that the page is simply refreshing under the condition that $_POST or $_GET exists, falling into one of the later header("Location: ...") commands?
If so, you'd want to fix the problem by adding a die();
if (!isset($_SESSION['username'])) { header("Location: index.php"); die(); }
Using exit() or die functions may fix the problem. But there is only very very limited amount of situations where actually need to use one of these functions.
I think you can enhance if else conditions by putting some more conditions. But this will increase your lines of code.
From my experience, every time there is redirect via headers, its following connected code tends to execute.
For example : if you have an else/else if along with an if(which has the redirect code) then they will also be executed and the redirect never happens. However if you break up the conditions into individual ifs then after entering one if if a redirect is present such that there is no succeeding code after that header code in the if then the redirect will happen.
Better to use die()/exit() all over to avoid discrepancies.

Categories