PHP Goto work around needed please - php

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

Related

PHP not performing

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!

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.

How to reload a PHP page twice before displaying it completely to a user?

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.

Redirecting URL with elseif $_SERVER['PHP_SELF']

I have a main page that users go to that shows output from a MySQL query depending on the variable passed to it. So;
http://website/mypage.php?page=0
However, I would like to set up redirection so that if someone just goes to
http://website/mypage.php
that it will go to http://website/mypage.php?page=0. I thought of using the following code, which verifies the current page as well as verifies that a user's session is established;
elseif ($_SERVER['PHP_SELF'] == '/mypage.php' && isset($_SESSION['valid_user']))
{
header('Refresh: 0; URL=/mypage.php?page=0');
}
But, this looks to be too general. Is there a way to check for exactly '/mypage.php' or maybe '/mypage.php?page=' ? I thought of using strlen to check for only the 11 characters in /mypage.php, but I'm not sure that this is the most efficient way of doing it.
You can check to see if the variable page has a value and if its empty you can do the redirect
if($_GET['page'] == ''){
header('Location: /mypage.php?page=0');
exit;
}
or
if(!isset($_GET['page'])){
header('Location: /mypage.php?page=0');
exit;
}
In your mypage.php you can check something like this
if(!isset($_GET['page'])) {
header('location: /mypage.php?page=0');
exit;
}
But, I think, instead of redirecting to same page with a get variable, why don't just show the page you want to show by default, when there is no page variable is set.
You should use this way:
header('Location: /mypage.php?page=0');
exit;
Otherwise check the $_SERVER variables for more strict match. http://php.net/manual/en/reserved.variables.server.php
I think probably you need $_SERVER['REQUEST_URI']
Is this what you're looking for?
if(!isset($_GET['page']) || empty($_GET['page'])) {
Header('Location: http://website/mypage.php?page=0')
exit();
}
or
if(!isset($_GET['page']))
$_GET['page'] = 0;
I'm not sure about the second solution, you shouldn't attribute value to $_GET[] variables.
if( !isset($_REQUEST['page']) ) {
header('Location: /mypage.php?page=0');
exit(0);
} else {
if( $_REQUEST['page']=="" ) {
header('Location: /mypage.php?page=0');
exit(0);
}
}

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