i can use this script easily when users land on site.com/redirect.php
they get redirected to appropriate TLD according to geo IP
but when i add this code in 'index.php' it creates a redirect loop.
Can you help me modify it so that it doesn't create loop. right now this 'break' is not helping..
<?php
// Next two lines are for Beyond Hosting
// Don't forget to change your-domain
require_once '/home/your-domain/php/Net/GeoIP.php';
$geoip = Net_GeoIP::getInstance('/home/your-domain/php/Net/GeoIP.dat');
// Next two lines are for HostGator
require_once 'Net/GeoIP.php';
$geoip = Net_GeoIP::getInstance('GeoIP.dat');
try {
$country = $geoip->lookupCountryCode($_SERVER['REMOTE_ADDR']);
switch((string)$country) {
case 'AU':
$url = "http://www.site.au";
break;
case 'CA':
$url = "http://www.site.ca";
break;
default:
$url = "http://site.com";
}
header('Location: '.$url);
} catch (Exception $e) {
// Handle exception
}
?>
You should check if the user is visiting the site via the localised URL before forwarding:
<?php
// Next two lines are for Beyond Hosting
// Don't forget to change your-domain
require_once '/home/your-domain/php/Net/GeoIP.php';
$geoip = Net_GeoIP::getInstance('/home/your-domain/php/Net/GeoIP.dat');
// Next two lines are for HostGator
require_once 'Net/GeoIP.php';
$geoip = Net_GeoIP::getInstance('GeoIP.dat');
try {
$country = $geoip->lookupCountryCode($_SERVER['REMOTE_ADDR']);
switch((string)$country) {
case 'AU':
$url = "http://www.site.au";
break;
case 'CA':
$url = "http://www.site.ca";
break;
default:
$url = "http://site.com";
}
if (strpos("http://$_SERVER[HTTP_HOST]", $url) === false)
{
header('Location: '.$url);
}
} catch (Exception $e) {
// Handle exception
}
?>
use this
header("Location:".$url);
can you do something like: (WARNING, i haven't tested this code, but the logic should be like this)
//Inside your try:
$country = $geoip->lookupCountryCode($_SERVER['REMOTE_ADDR']);
$serverName = explode('.', $_SERVER['SERVER_NAME']);
$serverCountryCode = $serverName[count($serverName)-1];
if (strtoupper ($serverCountryCode) != $country)) {
$shouldRedirect = true;
switch((string)$country) {
case 'AU':
$url = "http://www.site.au";
break;
case 'CA':
$url = "http://www.site.ca";
break;
default:
if ($serverCountryCode == 'com') {
$shouldRedirect = false;
}
$url = "http://site.com";
}
if ($shouldRedirect) {
header('Location: '.$url);
}
}
Related
I have two WordPress sites using the multi-site function, the URLs are below:
A: sample.com
B: sample.com/en
I tried to write a code in PHP following these conditions, but when I access the RUL of A:sample.com, a browser(chrome) shows an error.
So would you mind telling me how should I solve this problem?
Thank you in advance.
the conditions for access
The first access is only to [A: sample.com]
Users whose browser language is set to Japanese access to [A:
sample.com]
All users whose browser language setting is not set to Japanese
access [B: sample.com/en]
The errors messages in the browser(chrome)
This page isn’t working
sample.com redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS
The code for adding in functions.php
<?php
$uri = $_SERVER['REQUEST_URI'];
$BASE_LANG = 'en';
if (!preg_match('/^[!-~][a-zA-Z]{2}[!-~]/', $uri)) {
$languages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
$lang = $BASE_LANG;
if (isset($languages)) {
$browser_lamguage = $languages[0];
$base_languages = array('ja', 'en');
foreach ($base_languages as $base_language) {
if (preg_match("/^$base_language/i", $browser_lamguage)) {
$lang = $base_language;
break;
}
}
}
$url = get_site_url()."/$lang/";
if ($lang == 'ja') {
$url = get_site_url();
}
header("Location: $url");
exit();
}
?>
Development environment
CentOS (7 x86_64)
Apache (2.4.6 CentOS)
PHP (7.1.33)
wordpress(5.2.5)
Just make below change in your code:
if ($lang != 'ja') {
header("Location: $url");
exit();
}
Edited:
$uri = $_SERVER['REQUEST_URI'];
$BASE_LANG = 'en';
if (!preg_match('/^[!-~][a-zA-Z]{2}[!-~]/', $uri)) {
$languages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
$lang = $BASE_LANG;
if (isset($languages)) {
$browser_lamguage = $languages[0];
$base_languages = array('ja', 'en');
foreach ($base_languages as $base_language) {
if (preg_match("/^$base_language/i", $browser_lamguage)) {
$lang = $base_language;
break;
}
}
}
$url = get_site_url()."/$lang/";
if ($lang != 'ja') {
header("Location: $url");
exit();
}
}
I have this URL (part of the url):
index.php?option=com_umpai&status=success
When status = success is pass to component, i want it to run success task.
So i created a router php file to get status and put in task. Not sure if this is a correct method to do it? How can i put in the task by getting status in my url?
I am wondering does router.php apply to url type in the address bar?
This is my router codes:
<?php
defined('_JEXEC') or die('Restricted access');
function UmpaiBuildRoute(&$query)
{
$segments = array();
if(isset($query['status']))
{
switch($query['status']) {
case 'success':
$segments[] = 'success';
case 'fail':
$segments[] = 'fail';
case 'cancel':
$segments[] = 'cancel';
}
$segments[] = $query['task'];
unset($query['task']);
unset($query['status']);
}
return $router->build($segments);
}
function UmpaiParseRoute($segments)
{
$vars = array();
$count = count($segments);
if(!empty($count)) {
if($segments[0] == 'success'){
$vars['task'] = 'success';
}
}
return $router->parse($vars);
}
This is the simple code version to test if my router is working, but it is not working too:
function UmpaiBuildRoute(&$query)
{
$segments = array();
$segments[] = 'success';
return $segments;
}
function UmpaiParseRoute($segments)
{
$vars = array();
$count = count($segments);
if(!empty($count)) {
if($segments[0] == 'success'){
$vars['task'] = 'success';
}
}
return $vars;
var_dump($vars);
}
I tried this as well:
function UmpaiBuildRoute(&$query)
{
$segments[] = $_GET['status'];
}
function UmpaiParseRoute($segments)
{
$vars = array();
$count = count($segments);
if(!empty($count)) {
if($segments[0] == 'success'){
$vars['task'] = 'success';
}
}
return $vars;
}
Verhaeren (in the comments for the question) is right, try this:
if(!empty($_GET['status'])) {
// if status is set:
switch($_GET['status']) {
case 'success':
$segments[] = 'success';
case 'fail':
$segments[] = 'fail';
case 'cancel':
$segments[] = 'cancel';
}
} else {
// if status is not set...
};
Note: instead of this switch, you could also use $segments[] = $_GET['status'];
$_GET is an array but $_GET['status'] is a string. $_GET is an array with all URL variables (if any).
function UmpaiParseRoute()
{
$vars = array();
if(isset($_GET['status'])) {
$vars['task'] = 'success';
}
return $vars;
}
Also notice you don't need to pass $_GET as a parameter because is a SUPERGLOBAL variable.
I'm try to send a value to a PHP file, but when I check, this value became null.
I send the value by: user_login.php?p_action=New_User
The code of user_login.php is:
require("include/session_inc.php");
require("include/user_handling_inc.php");
require("include/db_inc.php");
start_Session(false, false);
switch ($p_action) {
case 'Login': {
$l_flag = verify_User($p_in_username, $p_in_password);
if ($l_flag == "Not_Found") {
$l_flag = "New_User";
}
break;
}
case 'Save_Profile': {
$l_flag = "Save_Profile";
break;
}
case 'New_User':
$l_flag = "New_User";
break;
case 'Create_New_User':
$l_flag = "Create_New_User";
}
switch ($l_flag) {
case 'New_User': {
include "include/user_new_inc.php";
break;
}
case 'Save_Profile': {
load_User_Data(" username = '$p_in_username' ", false);
include "include/user_profile_save_inc.php";
break;
}
case 'Wrong_Password':
echo "Wrong Pass";
break;
case 'OK':
load_User_Data(" username = '$p_in_username' ", true);
store_User_Cookie($g_userdata->user_id);
include "include/user_profile_inc.php";
break;
case 'Create_New_User':
$l_user_id = create_New_User ($p_in_username, $p_in_email, 'Y');
if ($l_user_id != -1) {
store_User_Cookie($l_user_id);
echo "Success !! <br><br> \n";
echo "<a href\"/index.php\"> Back to Main </a>";
}
break;
}
First your code isn't correct please read more about using Switch here
second to access to any variable came from url you can use Global variable $_GET or $_REQUEST
and you can read more about them from here and here
and this is your code after fixing it please try to run it
<?php
require("include/session_inc.php");
require("include/user_handling_inc.php");
require("include/db_inc.php");
start_Session(false, false);
$p_action=$_GET["p_action"];
switch ($p_action) {
case 'Login':
$l_flag = verify_User($p_in_username, $p_in_password);
if ($l_flag == "Not_Found") {
$l_flag = "New_User";
}
break;
case 'Save_Profile':
$l_flag = "Save_Profile";
break;
case 'New_User':
$l_flag = "New_User";
break;
case 'Create_New_User':
$l_flag = "Create_New_User";
break;
}
switch ($l_flag) {
case 'New_User':
include "include/user_new_inc.php";
break;
case 'Save_Profile':
load_User_Data(" username = '$p_in_username' ", false);
include "include/user_profile_save_inc.php";
break;
case 'Wrong_Password':
echo "Wrong Pass";
break;
case 'OK':
load_User_Data(" username = '$p_in_username' ", true);
store_User_Cookie($g_userdata->user_id);
include "include/user_profile_inc.php";
break;
case 'Create_New_User':
$l_user_id = create_New_User ($p_in_username, $p_in_email, 'Y');
if ($l_user_id != -1) {
store_User_Cookie($l_user_id);
echo "Success !! <br><br> \n";
echo "<a href\"/index.php\"> Back to Main </a>";
}
break;
}
?>
you need to make the code like this friend
switch ($_GET["p_action"]) {
case 'Login': {
$l_flag = verify_User($p_in_username, $p_in_password);
if ($l_flag == "Not_Found") {
$l_flag = "New_User";
}
that well give you the value of the get!!!
Use $_GET to get your parameter.
Sometimes $_REQUEST is preferable since it access both get & post data.
2nd thing never trust the user input so you must use addslashes(); or real_escape_string() function to prevent attacks on the system.
So Code would be like this :
$var = addslashes($_GET['p_action']);
switch($p) {
case 'Login':
$l_flag = verify_User($p_in_username, $p_in_password);
if ($l_flag == "Not_Found") {
$l_flag = "New_User";
}
break;
"OTHER CASES HERE"
}
Notice that : Don't add { } for CASE. Read syntax for switch
here.
i have a page with 4 links namely 1st.php, 2nd.php, 3rd.php, and 4th.php. my index.php displays the 3rd.php as default. i also created a 404.php (error page) so that when a user edits the URL it will not redirect to the index.php, instead it will display the error page. the problem is when i open my index.php page, it displays the error page instead. need help. this is my code:
<?php
$quarters = array('Q1', 'Q2', 'Q3', 'Q4');
$quarter = 'Q3';
if(isset($_GET['quarter']) && in_array($_GET['quarter'], $quarters)) {
$quarter = $_GET['quarter'];
}
switch($quarter) {
case 'Q1' :
$quarter = 'firstq2012.php';
break;
case 'Q2' :
$quarter = 'secondq2012.php';
break;
case 'Q3' :
$quarter = 'thirdq2012.php';
break;
case 'Q4' :
$quarter = 'fourthq2012.php';
break;
}
$pages = array('Q1','Q2','Q3','Q4');
if (in_array($_GET['quarter'], $pages)){
include_once $quarter;
}
else {
header('Location: 404.php');
}
?>
Your code can be cleaned up a lot.
Try using this intead:
<?php
$quarters = array('Q1' => 'firstq2012.php', 'Q2' => 'secondq2012.php', 'Q3' => 'thirdq2012.php', 'Q4' => 'fourthq2012.php');
if(isset($_GET['quarter'])) {
if(in_array($_GET['quarter'], array_keys($quarters))) {
include_once $quarters[$_GET['quarter']];
} else {
header('Location: 404.php');
}
} else {
include_once $quarters['Q3'];
}
?>
Why do You add GET data to quarter if after that You re-initialize $quarter?
And re-initialized $quarter stores incorrect filenames, if You have 1st.php not firstq2012.php.
And if (in_array($_GET['quarter'], $pages)){
include_once $quarter;
}
else {
header('Location: 404.php');
}
If $_GET['quarter'] does not exists, which would be if opens just index.php not index.php?q=..., then it redirects.
I suggest that in Your switch...case You create variable $filename = '1st.php';
And then check if (in_array($quarter, $pages)) { include_once $filename; } else { ... }
I have a html form with text input field. I'm wondering how to recognize specific input from the form. Example input commands:
<input type="text" name="action" value="bookmark http://google.com" />
<?php
if ($command == "goto"):
// go to website X
elseif ($command == "bookmark"):
// bookmark website X
else:
// something else
endif;
?>
I think the easiest way is to split the string on the first space to separate it into a command and the parameter for that command. The "2" parameter to explode() allows spaces to be used in $param, if necessary.
$input = explode(' ', $_POST['action'], 2);
$command = $input[0];
$param = $input[1];
switch ($command) {
case 'goto':
// go to website $param
break;
case 'bookmark':
// bookmark website $param
break;
default:
// unknown command
}
$aAct = explode(' ', $_POST['action');
if(is_array($aAct)) {
switch($aAct[0]) {
case 'bookmark':
/* do action e.g. header('Location: ' . $aAct[1]); */
break;
}
}
Make a case/break combination for every action you intend to specify..
Something like this?:
//get the command from your value
$command = current(explode(" ", $_POST['action']));
//get the url from your value
$url = next(explode(" ", $_POST['action']));
And as stated by karim79, a switch to handle the input would more appropriate.
switch($command) {
case 'goto':
// do stuff with $url;
break;
case 'bookmark':
// do stuff with $url;
break;
default: // do something default;
}
hope it helps
Try this:
$request = $_POST['action'];
$split = explode(' ',$request,2);
$command = $split[0];
if(!isset($split[1])){
//no url
die;
}
$url = $split[1];
if($command == "goto"){
header('location: '.$url);
die;
}elseif($command == "bookmark"){
header('location: '.$url);
die;
}else{
echo 'No Commands :(';
}
Use $_POST or $_GET to retrive the request data. ie: $_GET['action']
Set the header location to redirect the browser. die; or exit; is used to terminate and output the current script