I'm trying to make a multi-regional website using WordPress. i'm working on url http://localhost/siteone I want when someone come from usa redirect to the localhost/siteone/usa/
I edit the function.php file of wordpress theme with code below, but getting error the url become localhost/siteone/usa/usa/usa/usa
// Detacting user location using ipstack
$json = file_get_contents('http://api.ipstack.com/check?access_key=ACCESSKEY&language=en');
// Converts it into a PHP object
$data = json_decode($json);
$loc = $data->country_code;
//NA = northamerica
//End of Decating user location
if($loc=="NA"){ header("Location: usa/"); }
This works very well sidealone but when I add in function.php in theme file not working and give me a error. what should I do. Should i use some session or anything eles.
It looks like it's trying to redirect multiple times, since it'll check the person's location, then redirect, then after redirecting it'll check again, and redirect again, etc.
What you could do is check whether "/usa" is already in the page URL, and if not, redirect, something like this could work:
if ($loc === "NA" && strpos($_SERVER['REQUEST_URI'],'usa/') === false) {
header("Location: usa/");
}
$_SERVER['REQUEST_URI'] is the URL of the current page, not including the domain.
For languages and create diferents regionals website, I'd recommend you this plugins (pro):
https://polylang.pro/ (just one paid)
enter link description here (anual fee)
Custom code based on language:
<?php
if(pll_current_language() == 'en') {
echo 'Only in english';
} else if(pll_current_language() == 'fr') {
echo 'Seulment en francais';
}
?>
If you prefer control language throught web server, use better: $_SERVER['HTTP_ACCEPT_LANGUAGE']
Link: https://www.php.net/manual/en/locale.acceptfromhttp.php
Regards!
Related
I'm making a Wordpress function that redirects the user to a different page when they're on a certain IP address. The code however does not function properly and I can't get it to work.
function ip_based_login()
{
if ($_SERVER['REMOTE_ADDR'] == '95.81.51.134')
{
wp_redirect("examplewebsite.com/login2"(site_url($wp->request)));
}
else
{
exit;
}
}
you dont really need to write a code for that
The best thing about wordpress is that there are tons of stuff already available
You can use the following plugin for your requirements
https://wordpress.org/plugins/shortcode-redirect/
site_url expects a path to attach to the site url(eg: site_url('hello') => https://example.com/hello.
$wp->request contains the path of the request which means doing site_url($wp->request) will return you the url you are on.
here is a working snippet, just replace $url with the url you want to redirect to.
function ip_based_login() {
$visitor = $_SERVER['REMOTE_ADDR'];
$redirectTo = site_url('login2');
if (preg_match("/95.81.51.134/",$visitor)) {
wp_redirect($redirectTo);
}
exit;
}
keep in mind that placing this snippet in functions.php without hooking it to a specific action can cause an infinite redirect loop.
Please help me, and be patient since i'm a php virgin.
I'm using Cloudflare and want to redirect every page from outside of Indonesia to a specific page. After a few tries, I can redirect from index.php to each separate destination page.
But the problem comes when I put this redirect code on the destination page. It seems like it loops indefinitely. I want to keep this functionality on, because I don't want a visitor that clicks on a Google search result to end up in the wrong page.
How can I properly check the IP on every page, and if they are from ID, then they will be redirected to index_id.php. And if they are not, they will go to index_ww.php. This without infinite loops, because this code is in both index_id.php and index_ww.php.
$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];
if ($country_code=="ID"){
$link = 'http://www.myweb.com/index_id.php';
} else {
$link = 'http://www.myweb.com/index_ww.php';
}
header("location:$link");
exit;
Try adding a query parameter onto the end of the destination URL, like ?redirected=1, and always check for that parameter before performing the redirect code. Here's an example:
<?php
if (empty($_GET['redirected'])) {
$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];
if ($country_code=="ID"){
$link = 'http://www.myweb.com/index_id.php?redirected=1';
}
else {
$link = 'http://www.myweb.com/index_ww.php?redirected=1';
}
header("location:$link");
exit;
}
?>
I have several folders on my domain, within each folder contains an index.php file that checks to see if the database connection passes or fails, if it fails, the page is redirected to a top level file (outside of all folders) called offline.php. This part works great. The basic format I'm using to redirect if the db is offline is:
if ( !$dbconnect ) {
header("Location: https://www.test.com/offline.php");
}
Then, within the offline.php page, I need to check to see which folder brought the user to the offline.php page, and display a unique message to the user - based on the folder that brought them to the offline.php page.
For example:
test.com/test1/index.php redirects to offline.php, the message would say 'test1 brought you to this page'
test.com/test2/index.php redirects to offline.php, the message would say 'test2 brought you to this page'.
In multiple browsers I've tried the following code, which always results in 'unknown uri':
$url = 'https://' . $_SERVER['HTTP_REFERER'] ;
if ( strpos($url,'test') !== false ) {
echo 'test';
} elseif ( strpos($url,'test1') !== false ) {
echo 'test1';
} elseif ( strpos($url,'test2') !== false ) {
echo 'test2';
} else {
echo 'unknown uri';
}
Suggestions?
EDIT
Due to the unreliable nature of HTTP_REFERER I've decided to put all of the conditions within the index.php page and forget about the offline.php page. A HUGE thank you to everyone who offered suggestions!
Why would you use redirects at all? They are heavy on the server, slow and just plain old unnecessary. Use a switch statement and have 1 controlling page instead of multiple folders and pages.
If you use the following code on your offline.php page, you can see all of the $_SERVER variables available (referring URL is in there)
echo '<pre>',print_r($_SERVER),'</pre>';
From there, you can take $_SERVER['HTTP_REFERER'] use a select case, or if then statement and accomplish your goal.
Based on some of your questions in the comments and people pointing out the use of $_SERVER['HTTP_REFERER'] being unreliable, you could do something like this instead.
On your index.php page with the dbconnect check, you could modify it to be something like this. header("Location: https://www.test.com/offline.php?org=".urlencode($_SERVER['REQUEST_URI']));
Then, on the offline.php,
$page = urldecode($_GET['org']);
$org = explode('/',$page);
echo $org[1] to get the first value after the slash, $org[2] would get the next value etc..
Let say we've the following
Objective : User will post certain exact URL $refere to lock viewing text content and only be allowed for view if the viwer is coming from the same exact URL $refere.
$refere = "http://www.site_site.com"; // User will post it
$r = $_SERVER['HTTP_REFERER']; // To get real referral
and i want to do the following
<?PHP
if(stripos($r, $refere) == false){
echo "Wrong";
} else { ?>
echo "Go";
}
?>
It always gives me $r = $_SERVER['HTTP_REFERER']; blank ! so does it deprecated on any PHP version 4 or 5 whatever !
Also
what is the user posted $refere like https:// or missed www. or only posted site_site.com while the $r = $_SERVER['HTTP_REFERER']; showing www.site_site.com
so can anyone help me to adjust this code to be working fine no matter the user posted the $refere link fully or only site_site.com.
The $_SERVER['REFERER'] variable will only be set when you click a link to your page from another page and if the browser (or an eventual proxy or firewall you're on) isn't removing the referer header.
To your second question: do some string comparisons. The functions strpos() and substr() will be of great help.
All my full site pages are under folder public_html, which contains index.php and folder m which contains all the mobile site pages. index.php is like this:
<?php
if (...mobile client) {
header('Location: m/');
} else {
include 'front.html.php';
}
?>
There is also an index.php in m, simply `include 'front.html'.
This script can detect user's client and direct to full site and mobile site automatically.
But now I want a link on the mobile site to switch to full site. If it is like switch to full site, there will be front.html.php in the address bar, and I don't want this. However, if it is like switch to full site, it will detect again and back to mobile site.
How to deal with that?
You can accomplish this by setting a cookie.
setcookie("forceClient", "full", time()+3600);
Then from that php script, redirect to the home page.
In your index.php, check the cookie:
if($_COOKIE["forceClient"] == "full"){
//logic
}
Make a session variable
like
$_SESSION['viewer_type'] = "Mobile";
or
$_SESSION['viewer_type'] = "Regulare";
and then define a new variable call it base_url and save it in the session also
and do this
if($_SESSION['viewer_type'] == 'Mobile'):
$_SESSION['base_url'] = "http://www.m.site.com/";
else:
$_SESSION['base_url'] = "http://www.site.com/";
endif;
the link now will be
Test
You need to set the session variable each time the view changed from mobile to regular or visa versa