Grab URL using PHP - php

I have a script that grabs the current page url & stores it in a variable:
$page = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
I then check this URL for a phrase to check what page the user is on.
if (strpos($page,'celtic') !== false) { echo 'this is the celtic page'; }
However, I have found a bug in this.
If the domain contains the phrase celtic, it won't work.
How can I test just the 3rd part of the URL so it would return true if the domain was www.celticfootball.com/teams/scotland/celtic/.

You can try do smth like this:
$params = $_SERVER['REQUEST_URI'];
switch(true) {
case (bool) strpos($params, 'celtic'):
echo 'celtic';
break;
case (bool) strpos($params, 'another'):
echo 'another';
break;
default:
//ignore
break;
}

Related

Redirect from facebook causing error because of added address ending

I have created in my website simple file routing.php and created switch to redirect to file depends from url. My routing.php looks like:
<?php
$request = $_SERVER['REQUEST_URI'];
// $request = substr($request, 0, strpos($request, "?"));
switch ($request) {
case '/' :
require_once 'index.php';
break;
case '/o-inwestycji' :
require_once 'about.php';
break;
case '/galeria' :
require_once 'gallery.php';
break;
case '/lokalizacja' :
require_once 'localisation.php';
break;
case '/kontakt' :
require_once 'contact.php';
break;
case '/login' :
require_once 'login.php';
break;
}
?>
My problem is when I will send a link to website in facebook. When user will click link facebook add to link additional parameter in url and my urls looks like:
www.example.com/login?fbclid=IwAR3xVjfqiDVOiqc9p3b2wtl5gL_OBvlLN3aWTWgWMiHGEgIx8TagsGxHkqI
That mechanism cause error in my website and user is redirected to blank page. I tried to delete all characters after ? but that's not working always well. Could you help me to solve that problem? Thank you!
The issue with:
$request = substr($request, 0, strpos($request, "?"));
is that if there is no ? present strpos evaluates to false and comes out as you wanted the string to be 0 length. e.g. the same as:
echo substr('test', 0, false);
You can change that so you check if there is a ? before using:
$request = strpos($request, "?") !== FALSE ? substr($request, 0, strpos($request, "?")) : $request;
Additionally, you should add a default case to your switch so when a match doesn't occur the user ends up in an expected location.
You could probably get rid of:
case '/' :
require_once 'index.php';
break;
case '/login' :
require_once 'login.php';
break;
and use:
default:
require_once 'login.php';
after all the other cases. You can read more about this here, https://www.php.net/manual/en/control-structures.switch.php.

How to simlify php code by using arrays from url strings

i'm new at this forum and php.
I want to show some info when the script detects one or more words in the postname (Wordpress)
In my exapmle like to dispaly extra info when Omnik + reset or wifi is detected.
I like to know how i can simplify the following code:
$url = "www.myurl.nl/postname"
if (strpos($url, 'omnik' )!==false){
echo "Omnik";
}
else if (strpos($url, 'reset' )!==false){
echo "Reset";
}
else if (strpos($url, 'wifi' )!==false){
echo "Wifi";
}
else {
echo "No Omnik,Reset or Wifi there";
}
At this moment i can only show the extra info when the word "Omnik" is detected.
Example: https://geaskb.nl/omnik shows the extra info, but https://geaskb.nl/omnik-reset and https://geaskb.nl/omnik-wifi should show the info too, while https://geaskb.nl/solaredge shouldn't show the info.
Hope you get what i mean.
====== Added 20:00 ========
Hi All, thanks for the ansewers.
I should have be clear the 1st time i guess.
This is the code i use now:
// Verkrijg URL incl. subdir.
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')
$geturl= "https";
else
$geturl = "http";
// Here append the common URL characters.
$geturl .= "://";
// Append the host(domain name, ip) to the URL.
$geturl .= $_SERVER['HTTP_HOST'];
// Append the requested resource location to the URL
$geturl .= $_SERVER['REQUEST_URI'];
if (strpos($_SERVER['REQUEST_URI'], "omnik" )!==false){
echo "Omnik in url";
}
else {
echo "Geen Omnik in $geturl";
}
============= 20:30u ==================
Problem solved!! stripos solved the problem!
Thanks for all your help!
One way for doing it through foreach loop
<?php
$url = "www.myurl.nl/postname";
$needles = ['omnik', 'reset', 'wifi']; // Add more if needed
foreach($needles as $needle){
if (strpos($url, $needle )!==false){
echo $needle;
}
}
?>

PHP - Change every url in html depending on domain

Using PHP I would like to be able to change every url in a specific HTML page depending on domain.
This code allows me to change urls one by one but I'm sure there is a better way:
$domain = $_SERVER['SERVER_NAME'];
switch ($domain) {
case "example2.com":
$url1 = "http://".$domain."/secondsubfolder/thispage.html";
break;
case "example3.com":
$url1 = "http://".$domain."/thirdsubfolder/thispage.html";
break;
default:
$url1 = "http://example.com/firstsubfolder/thispage.html";
}
HTML:
first url
Notice that I also need to change the first subfolder depending on domain, thats why I can't use relative urls.
So my aim is to be able change every url in my default HTML code:
hello
bye
should change if my website is accessed through example2.com
<a href="example2.com/secondsubfolder/thispage.html" >hello</a>
bye
Your question isn't very clear to me but I guess you need something like:
<?php
$domain = $_SERVER['SERVER_NAME'];
$scriptPath = $_SERVER['SCRIPT_NAME'];
switch ($domain) {
case "example2.com":
$domain = "example2.com";
$folder = "secondsubfolder";
break;
case "example3.com":
$domain = "example3.com";
$folder = "thirdsubfolder";
break;
default:
$domain = "example.com";
$folder = "thirdsubfolder";
}
$scriptPath = preg_replace('%^/.*?/%', "/$folder/", $scriptPath);
echo "http://{$domain}{$scriptPath}";

punycode and .рф cyrillic domain redirect

I have a website with cyrillic domain name. There is an authorization lib which redirects the user to login page, but the url is somehow missformed.
The website is on CodeIgniter and the redirect function used is the standard redirect function of
the codeigniter. I have modified a bit
and it looks now like this
function redirect($uri = '', $method = 'location', $http_response_code = 302)
{
if ( ! preg_match('#^https?://#ui', $uri))
{
$uri = site_url($uri);
}
//exit(idn_to_ascii($uri));
switch($method)
{
case 'refresh' : header("Refresh:0;url=http://".idn_to_ascii($uri));
break;
default : header("Location:http://".idn_to_ascii($uri), TRUE, $http_response_code);
break;
}
exit;
}
idn_to_ascii functions seems not rightly coding the UTF url string ...
Can anybody hint a solution?
Instead of
http://xn-------63dat7alb0aizbbjcoujt7j3a6e.xn--p1ai/auth/admin/
I get
xn-------63dat7alb0aizbbjcoujt7j3a6e.xn--/auth/login-foj4c
Any ideas ?
You should change your code a bit and use idn_to_ascii() only on domain part and not on the full url.
instead of:
switch($method)
{
case 'refresh' : header("Refresh:0;url=http://".idn_to_ascii($uri));
break;
default : header("Location:http://".idn_to_ascii($uri), TRUE, $http_response_code);
break;
}
you should use:
$pos = mb_strpos($uri,'/', null, 'UTF8');
if ($pos === false) { // only domain, no slash here
$uri = idn_to_ascii($uri);
}
else { // changes only for domain part, rest left unchanged
$uri = idn_to_ascii(mb_substr($uri,0,$pos, 'UTF-8')).mb_substr($uri,$pos,null, 'UTF-8');
}
switch($method)
{
case 'refresh' : header("Refresh:0;url=http://".$uri);
break;
default : header("Location:http://".$uri, TRUE, $http_response_code);
break;
}
EDIT
Test code just for generating URL:
<?php
$uri = 'помощь-от-сглаза-и-порчи.рф/auth/admin/';
$pos = mb_strpos($uri,'/', null, 'UTF8');
if ($pos === false) {
$uri = idn_to_ascii($uri);
}
else {
$uri = idn_to_ascii(mb_substr($uri,0,$pos, 'UTF-8')).mb_substr($uri,$pos,null, 'UTF-8');
}
echo $uri."<br />";
Output for this is: xn-------63dat7alb0aizbbjcoujt7j3a6e.xn--p1ai/auth/admin/ as expected I think - when I copy this text into browser I get redirected again to http://помощь-от-сглаза-и-порчи.рф/auth/admin/

Geographically change domain keeping URL same

i am using this script to redirect users according to their IP.
But the problem is it redirects to homepage or only to URL i specify in the script. how can i just change the domain keeping the URL same? for example site.com/whateverpage redirected to site.au/whateverpage.
<?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
}
?>
Add $_SERVER['REQUEST_URI'] to your redirection.
header("Location: $url/$_SERVER[REQUEST_URI]");

Categories