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/
Related
I try to make a clean url with a simpel php code found here at stackoverflow. URL rewriting with PHP.
exampel.com/foo works
exampel.com/foo/ makes a 500 internal error
exampel.com/foo/bar also a 500.
I have tryed to figure why but i cant find the error. (i am not a php guru, but i belive i do understand some.)
my code to handel this fallback in index.php (fallback set in htacces)
$YdelseMenu = NULL;
function VisYdelser($element){
if(empty($element[0])) { // No path elements means home
$page2 = 'ydelser';
} else switch(array_shift($element))
{
case 'traefaeldning':
$page2 = 'traefaeldning';
break;
case 'beskaering':
$page2 = 'beskaering';
break;
default:
header('HTTP/1.1 404 Not Found');
$page2 = '404';
}
return $page2;
}
// https://stackoverflow.com/questions/16388959/url-rewriting-with-php
$path = ltrim($_SERVER['REQUEST_URI'], '/'); // Trim leading slash(es)
$elements = explode('/', $path); // Split path on slashes
if(empty($elements[0])) { // No path elements means home
$YdelseMenu = true;
$page = 'home';
} else switch(array_shift($elements)) // Pop off first item and switch
{
case 'ydelser':
$YdelseMenu = true;
$page = VisYdelser($elements); // passes rest of parameters to internal function
break;
case 'kontakt2':
$page = 'kontakt';
break;
default:
header('HTTP/1.1 404 Not Found');
$page = '404';
}
This might be a kaybord error but i cant find it. the error only seems to happen then 'VisYdelse()' is called and passes on to the 'switch'?
I hope someone can see what i do wrong. And maby som tips to optimise the code will be nice:)
EDIT i made a err log but but its empty (testet the log, end it do work)
There is no problem with the code.
The error was in the htaccess.
Changed FallbackResource index.php
to FallbackResource /index.php
Note the leading slash.
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;
}
When i'm trying to redirect to other website, i receive this error:
A PHP Error was encountered
Severity: Warning
Message: parse_url(/%22**) [function.parse-url]: Unable to parse URL
Filename: core/URI.php
Line Number: 219
An Error Was Encountered
The URI you submitted has disallowed characters.
This is all the code i have in URI.php
private function _detect_uri()
{
if ( ! isset($_SERVER['REQUEST_URI']) OR ! isset($_SERVER['SCRIPT_NAME']))
{
return '';
}
$uri = $_SERVER['REQUEST_URI'];
if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
{
$uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
}
elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
{
$uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
}
// This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
// URI is found, and also fixes the QUERY_STRING server var and $_GET array.
if (strncmp($uri, '?/', 2) === 0)
{
$uri = substr($uri, 2);
}
$parts = preg_split('#\?#i', $uri, 2);
$uri = $parts[0];
if (isset($parts[1]))
{
$_SERVER['QUERY_STRING'] = $parts[1];
parse_str($_SERVER['QUERY_STRING'], $_GET);
}
else
{
$_SERVER['QUERY_STRING'] = '';
$_GET = array();
}
if ($uri == '/' || empty($uri))
{
return '/';
}
$uri = parse_url($uri, PHP_URL_PATH);
// Do some final cleaning of the URI and return it
return str_replace(array('//', '../'), '/', trim($uri, '/'));
}
CodeIgniter checks all URI segments for disallowed characters. This happens by white listing allowed characters. Which ones are allowed can be checked in /system/application/config/config.php in the $config['permitted_uri_chars'] variable. permitted_uri_chars are the characters that CodeIgniter accepts in your URI.The default value is set to something like.
$config['permitted_uri_chars'] = 'a-z 0-9~%.:&_\-';
By default only these are allowed: a-z 0-9~%.:_-
Leave blank to allow all characters -- but only if you are insane.
%22 comes for ".You can add this in permitted_uri_chars list.
Try this may help but is not recommended, in your application/config/config.php change:
$config['permitted_uri_chars'] = ''; #keep it blank to allow all characters
$config['allow_get_array'] = TRUE;
$config['enable_query_strings'] = TRUE;
In my case it was a mal-formed URL.
It was like mydomain/route¶m=1
Be aware that it should have an interrogation character instead of an "&" at the first parameter.
So it should be like this:
mydomain/route?param=1&other=2&another=3
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]");
This is my url:
http://localhost/framework/index.php
echo $_SERVER['REQUEST_URI'];
Would output: /framework/index.php
But If my url was:
http://localhost/framework/
The output would be:
/framework/
And If I move the file, yeah you get the idea.
How do I grab the content after folders/eventually index.php file? My idea is to have index.php as a front controller.
If I have:
http://localhost/framework/index.php/test/test
I only want the test/test part.
http://localhost/framework/test/test
I only want the test/test part.
You can automatically detect the base uri and remove it, leaving you with the test/test part.
if(!empty($_SERVER['PATH_INFO']))
{
// Uri info does not contain docroot or index
$uri = $_SERVER['PATH_INFO'];
}
else
{
if(!empty($_SERVER['REQUEST_URI']) && !empty($_SERVER['HTTP_HOST']))
{
$fullUrl = 'http://'
. ((isset($_SERVER['HTTP_HOST'])) ? $_SERVER['HTTP_HOST'] : '')
. ((isset($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] : '');
$uri = parse_url($fullUrl, PHP_URL_PATH);
}
else if(!empty($_SERVER['PHP_SELF']))
{
$uri = $_SERVER['PHP_SELF'];
}
}
$baseUri = substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], "/")+1);
$uri = str_replace($baseUri, '', $uri);
Edit: mAu's comment above is correct. I was under the assumption you was already using mod rewrite.