append URL string after domain name or file name - php

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"

Related

Getting started with PHP/Laravel - Adding Http:// and Https:// on to URL Path

I am attempting to have a input field for adding a website/url. All I want to require to successfully submit the form is www.domainname.com; however, after submission I want to add back on http:// or https:// if it was not added by the person submitting the form.
The validation is along the lines of the following,
public function name($id) {
$input = Input::all();
$validator = Validator::make(
$input,
array(
'website' => 'active_url',
)
);
}
For this to work an if statement is needed. I have tried inserting something along the lines of the code listed below, but have not had any luck.
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
$url = "http://" . $url;
}
I am fairly new to PHP and just starting to use Laravel, so I apologize in advance if there is any confusion or lack of information. Any help is appreciated.
You could simply just strip the scheme to begin with and then add it.
$url = preg_replace('#^https?://#', '', $url);
$url = "http://" . $url;
Or
$url = ltrim('http://', $url);
$url = ltrim('https://', $url);
$url = 'http://' . $url;
Or
if (!preg_match('#^http(s)?://#', $url)) {
$url = 'http://' . $url;
}
This should work:
if (stripos($url, "http://") === false && stripos($url, "https://") === false) {
$url = "http://" . $url;
}
stripos is case-insensitive, so it shouldn't matter if the user typed in lowercase letters or not in the url prefix.
I think no need for validation,.. maybe it could help you
#alix axel code
function addhttp($url) {
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
$url = "http://" . $url;
}
return $url;
}

Encode url in php

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

how to check content of request_uri

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);

Grabbing digits at end of URL using PHP

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;

PHP: Remove 'WWW' from URL inside a String

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'];

Categories