hello i would like to know how to avoid double postings in a request_uri.
as an example:
http://www.example.com/foo/foo/...
should only be:
http://www.example.com/foo/...
for example i would like to create a check that i need for a header-function that needs to be checked before it headers. it should not header if the url-string contains double postings. so the check would be something like:
$host = $_SERVER['HTTP_HOST'];
$url = $_SERVER['REQUEST_URI'];
$urlArray = explode('/', $url);
$urlArrayUnique = array_unique($urlArray);
$urlUnique = implode('/', $urlArrayUnique);
if (isset($_SESSION['a'])){
$var = $_SESSION['a'];
if($url !== $urlUnique){
header ('Location:'.$host.'/'.$var.'/'.$basename);
exit;
}
}
if there is someone who could help me out i really would appreciate.
thanks alot.
Try the below code, use explode and get unique and implode it into URL....
$url = $_SERVER['REQUEST_URI'];
$urlArray = explode('/', $url);
$urlArrayUnique = array_unique($urlArray);
$urlUnique = implode('/', $urlArrayUnique);
You can get dulicate values in $urlArray too...
You can modify this below code to suit as per your need:
$host = $_SERVER['HTTP_HOST'];
$url = $_SERVER['REQUEST_URI'];
$urlArray = explode('/', $url);
$lai = max(array_keys($urlArray));//here we get the max array index
if($urlArray[$lai-1] != $urlArray[$lai])header('Location:'.$host.'/'.$var.'/'.$basename);
Related
i am passing a url as a param to the next page. ?url=http://domain.com i would like to set additional param to a querystring or the url. but only if a specific domain exists in the querystring.
i tried
$url = preg_replace('{http://www.domain.com}','http://www.domain.com?foo=bar/',$_GET['url']);
but this is not working when there is a file name or other params.
any help is appreciated.
This will do what you want:
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; // the URL you want to inject the parameters into
$params = "new=yes!"; // the new parameters you want to add at the beginning
if (strpos($url, "?") !== false) {
list($url, $b) = explode("?", $url, 2);
$params = "$params&$b";
}
$url .= "?".$params;
Output: http://example.com/example.php?new=yes!&a=b&c=no
If you want to place some parameter to the beginning of the query string you can use parse_url function:
$url = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$parsed_url = parse_url($url);
$new_url = $parsed_url['path'] ."?foo=bar" . ((isset($parsed_url['query']))? urlencode("&").$parsed_url['query'] : "");
var_dump($new_url);
// the output: string(45) "http://www.domain.com?foo=bar%26param=value"
I have string url variable;
$url ="http://carkva-gazeta.org/римско-католическая-церковь/";
I need transform $url to:
"http://carkva-gazeta.org/%d1%80%d0%b8%d0%bc%d1%81%d0%ba%d0%be-%d0%ba%d0%b0%d1%82%d0%be%d0%bb%d0%b8%d1%87%d0%b5%d1%81%d0%ba%d0%b0%d1%8f-%d1%86%d0%b5%d1%80%d0%ba%d0%be%d0%b2%d1%8c/"
I have tried: rawurlencode($url);
and urlencode($url);
But result is:
http%3A%2F%2Fcarkva-gazeta.org%2F%D1%80%D0%B8%D0%BC%D1%81%D0%BA%D0%BE-%D0%BA%D0%B0%D1%82%D0%BE%D0%BB%D0%B8%D1%87%D0%B5%D1%81%D0%BA%D0%B0%D1%8F-%D1%86%D0%B5%D1%80%D0%BA%D0%BE%D0%B2%D1%8C%2F
$url = "http://carkva-gazeta.org/";
$url .= urlencode("римско-католическая-церковь");
echo $url;
Like so?
Probably that's the best solution:
$url ="http://carkva-gazeta.org/римско-католическая-церковь/";
$x = parse_url($url);
echo $x['scheme'].'://'.$x['host'].strtolower(str_replace('%2F','/',urlencode($x['path'])));
I've used also strtolower to make it lowercase as you wanted
Assuming that you are getting your URL string automatically/dynamically and that it is not a fixed string that you can simply split while writing your code, you'll want something like this
$url = "http://carkva-gazeta.org/римско-католическая-церковь/";
// in case it is https, we don't want to hardcode http
$scheme = parse_url($url, PHP_URL_SCHEME);
$host = parse_url($url, PHP_URL_HOST);
// do not encode the first '/' or the last '/'
$encodedPath = strtolower(urlencode(substr(parse_url($url, PHP_URL_PATH), 1, -1)));
$encodedUrl = $scheme . "://" . $host . "/" . $encodedPath . "/";
DEMO
I wanted to remove all occurrences of specific pattern of a parameter from a URL using preg_expression. Also removing the last "&" if exist
The pattern looks like: make=xy ("make" is fixed; "xy" can be any two letters)
Example:
http://example.com/index.php?c=y&make=yu&do=ms&r=k&p=7&
After processing preg_replace, the outcome should be:
http://example.com/index.php?c=y&do=ms&r=k&p=7
I tried using:
$url = "index.php?ok=no&make=ae&make=as&something=no&make=gr";
$url = preg_replace('/(&?lang=..&?)/i', '', $url);
However, this did not work well because I have duplicates of make=xx in the URL (which is a case that could happen in my app).
You don't need RegEx for this:
$url = "http://example.com/index.php?ok=no&make=ae&make=as&something=no&make=gr&";
list($file, $parameters) = explode('?', $url);
parse_str($parameters, $output);
unset($output['make']); // remove the make parameter
$result = $file . '?' . http_build_query($output); // Rebuild the url
echo $result; // http://example.com/index.php?ok=no&something=no
You could try using:
$str = parse_url($url, PHP_URL_QUERY);
$query = array();
parse_str($str, $query);
var_dump($query);
This will return to you the query as an array. You could then use http_build_query() function to restore the array in a query string.
But if you want to use regexp:
$url = "index.php?make=ae&ok=no&make=ae&make=as&something=no&make=gr";
echo $url."\n";
$url = preg_replace('/\b([&|&]{0,1}make=[^&]*)\b/i','',$url);
$url = str_replace('?&','?',$url);
echo $url;
This will remove all make in the URL
with rtrim you can remove last &
$url = rtrim("http://example.com/index.php?c=y&make=yu&do=ms&r=k&p=7&","&");
$url = preg_replace('~&make=([a-z\-]*)~si', '', $url);
$url = "index.php?ok=no&make=ae&make=as&something=no&make=gr";
$url = preg_replace('/(&?make=[a-z]{2})/i', '', $url);
echo $url;
Just by using preg_replace
$x = "http://example.com/index.php?c1=y&make=yu&do1=ms&r1=k&p1=7&";
$x = preg_replace(['/(\?make=[a-z]*[&])/i', '/(\&make=[a-z]*[^(&*)])/i', '/&(?!\w)/i'], ['?','',''], $x);
echo $x;
And the result is: http://example.com/index.php?c1=y&do1=ms&r1=k&p1=7
Hope this will be helpful to you guys.
On a page of mine, I have a GET as a URL of a website.
mypage.com/page.php?=URLHERE
On this URL, I need the ID at the very end of the URL
mypage.com/page.php?url=http://www.otherwebsite.com/something.php?id=%%%%%%%
These numbers are sometimes different amount of digits, so how would I do that?
My code:
$url = $_GET['url'];
Assuming the url parameter is a properly encoded URL, then useparse_url() to get the URL components and parse_str() to retrieve the id parameter from its query string.
$url = $_GET['url'];
// First parse_url() breaks the original URL up
$parts = parse_url($url);
// parse_str() parses the query string from the embedded URL
$query = parse_str($parts['query']);
// Finally, you have your full id parameter
$id = $query['id'];
assuming the the url has id at the begining of query_string
<?php
$url = $_GET['url'];
$url = basename($url);
$url =explode("?",$url);
$url = explode("=",$url[1]);
echo $url[1];
?>
try the parse_url
$url = parse_url($_GET['url'], PHP_URL_QUERY);
$query = explode('=', $url);
$id = $query[1];
I'd use the PHP function explode() http://php.net/manual/en/function.explode.php
For this example
$numbers = explode("=",$url);
$id_value = $numbers[2];
print $id_value;
Currently I am using parse_url, however the host item of the array also includes the 'WWW' part which I do not want. How would I go about removing this?
$parse = parse_url($url);
print_r($parse);
$url = $parse['host'] . $parse['path'];
echo $url;
$url = preg_replace('#^www\.(.+\.)#i', '$1', $parse['host']) . $parse['path'];
This won't remove the www in www.com, but www.www.com results in www.com.
preg_replace('#^(http(s)?://)?w{3}\.#', '$1', $url);
if you don't need a protocol prefix, leave the second parameter empty
$url = preg_replace('/^www\./i', '', $parse['host']) . $parse['path'];