PHP6
12-04-2008, 10:58 PM
Hello,
I have written simple function to join url and relative path together in one url (may be it will be useful):
private function JoinUrlAndPath($url, $path) {
if (substr($path, 0, 7) == 'http://') {
return $path;
} else if (substr($path, 0, 1) == '/') {
if (preg_match('#http://[^/]*#', $url, $match)) {
return $match[0].$path;
}
} else if (substr($path, 0, 2) == '..') {
// remove last slash in the url if such exists
if (substr($url, strlen($url)-1, 1) == '/')
$url = substr($url, 0, strlen($url)-1);
$url = substr($url, 0, strrpos($url, '/'));
if ($url == 'http:/') return false;
$path = substr($path, 3, strlen($path)-3);
return $this->JoinUrlAndPath($url, $path);
} else if (substr($path, 0, 2) == './') {
$path = substr($path, 2, strlen($path)-2);
return $this->JoinUrlAndPath($url, $path);
} else {
$domainPath = substr($url, 0, strrpos($url, '/'));
if ($domainPath == 'http:/') $domainPath = $url;
return $domainPath.'/'.$path;
}
return false;
}
It worked nice until now. Today I was parsing web page with submit form. Lets say submit form had action "index.php/do/login/" and page’s url was "http://example.com/index.php/do/authorize/".
Since the first url was treated as directory (it is ended with slash) the action was just simply appended to the web page's url. I get following: "http://example.com/index.php/do/authorize/index.php/do/login/". BUT it does not work...
I have checked all packages sent from my PC to the server and discover that Firefox sends data to "http://example.com/index.php/do/login/" instead of my url.
The question is: how browser understands that index.php was script and not a directory name? Why it does not append action to the url? What is the standards of parsing relative paths and urls?
Thanks in advance
I have written simple function to join url and relative path together in one url (may be it will be useful):
private function JoinUrlAndPath($url, $path) {
if (substr($path, 0, 7) == 'http://') {
return $path;
} else if (substr($path, 0, 1) == '/') {
if (preg_match('#http://[^/]*#', $url, $match)) {
return $match[0].$path;
}
} else if (substr($path, 0, 2) == '..') {
// remove last slash in the url if such exists
if (substr($url, strlen($url)-1, 1) == '/')
$url = substr($url, 0, strlen($url)-1);
$url = substr($url, 0, strrpos($url, '/'));
if ($url == 'http:/') return false;
$path = substr($path, 3, strlen($path)-3);
return $this->JoinUrlAndPath($url, $path);
} else if (substr($path, 0, 2) == './') {
$path = substr($path, 2, strlen($path)-2);
return $this->JoinUrlAndPath($url, $path);
} else {
$domainPath = substr($url, 0, strrpos($url, '/'));
if ($domainPath == 'http:/') $domainPath = $url;
return $domainPath.'/'.$path;
}
return false;
}
It worked nice until now. Today I was parsing web page with submit form. Lets say submit form had action "index.php/do/login/" and page’s url was "http://example.com/index.php/do/authorize/".
Since the first url was treated as directory (it is ended with slash) the action was just simply appended to the web page's url. I get following: "http://example.com/index.php/do/authorize/index.php/do/login/". BUT it does not work...
I have checked all packages sent from my PC to the server and discover that Firefox sends data to "http://example.com/index.php/do/login/" instead of my url.
The question is: how browser understands that index.php was script and not a directory name? Why it does not append action to the url? What is the standards of parsing relative paths and urls?
Thanks in advance