get main url part of big link [duplicate] - php

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Get part of url in php
i want to get http://aoup.net/manage/preForm/test.php
from this url
http://aoup.net/manage/preForm/test.php?op=Results&form_id=1&form_name=%D8%A7%D9%86%D8%AA%D8%AE%D8%A7%D8%A8%20%D8%AD%D9%88%D8%B2%D9%87%20%D8%A7%D9%85%D8%AA%D8%AD%D8%A7%D9%86%DB%8C%28%D8%AF%D9%88%D8%B1%D9%87%20228%29&hash=406ce38266577b8dff3102e476fdf587
this my php code not work correctly:
echo 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);

You can use parse_url function to achieve this: http://es2.php.net/manual/en/function.parse-url.php
For example:
<?php
$url = 'http://aoup.net/manage/preForm/test.php?op=Results&form_id=1&form_name=%D8%A7%D9%86%D8%AA%D8%AE%D8%A7%D8%A8%20%D8%AD%D9%88%D8%B2%D9%87%20%D8%A7%D9%85%D8%AA%D8%AD%D8%A7%D9%86%DB%8C%28%D8%AF%D9%88%D8%B1%D9%87%20228%29&hash=406ce38266577b8dff3102e476fdf587';
$parsed_url = parse_url($url);
$new_url = $parsed_url['scheme'] . '://' . $parsed_url['host'] . $parsed_url['path'];
echo $new_url;
Will print http://aoup.net/manage/preForm/test.php

echo $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];

Related

Replace part of URL within PHP variable

I am using PHP to set the current URL as a variable using
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
This would echo the string:
http://www.example.com/landing-page-demo/
I would like to replace the 'landing-page' part of the string with 'confirmation-page' and then save this updated URL as another variable.
I was thinking of using str replace, is this the most ideal method of doing this? Not sure how to approach the problem
Indeed, short of knowing regular expressions, str_replace will do the trick.
Perform str_replace on $_SERVER['REQUEST_URI']
Example:
$url = 'http://' . $_SERVER['SERVER_NAME'] . str_replace("landing", "confirmation", $_SERVER['REQUEST_URI']);
My current solution using Str Replace:
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; // Full URL var
$redirecturl = str_replace("landing","confirmation", $url);
Using str_replace is the easiest solution however you can do your logic with pathinfo($url)
$url = "http://www.example.com/landing-page-demo/";
$newURL = str_replace('landing', 'confirmation', $url);
echo $newURL;

Preventing adding link to another link

Hello I'm currently working with php to generate a menu with a own build CMS system.
I'm making a dynamic link with : $url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."/";
Than I'm adding . $row_menu['page_link'] from the database. At first it works perfect:
as example =
$row_menu['page_link'] = page2;
$url . $row_menu['page_link'];
it will return as example : http://example.com/page2
But when I click again, it adds page2 again like : http://example.com/page2/page2
How do i prevent this?
Thanks in advance!
Because at first time your $_SERVER['REQUEST_URI'] will be like http://example.com but when the user click on the link then the value of $_SERVER['REQUEST_URI'] would become http://example.com/page2.That's why it is appending two times.
Instead you can use HTTP_REFERER like
$url = $_SERVER['HTTP_REFERER'].$row_menu['page_link'];
Considering that your $_SERVER['HTTP_REFERER'] will results http://example.com.Also you can try like
$protocol = 'http';
$url = $protocol .'//'. $_SERVER['HTTP_HOST'] .'/'. $row_menu['page_link'];
REQUEST_URI will give you whatever comes after example.com, so leave that out all together.
$url = $_SERVER['HTTP_HOST'] . "/" . $row_menu['page_link'];
You can find a full list of the $_SERVER references here.
Try this:
$requested_uri = $_SERVER['REQUESTED_URI'];
$host = $_SERVER['HTTP_HOST'];
$uri_segments = explode('/',$requested_uri);
$row_menu['page_link'] = 'page2';
if($row_menu['page_link'] == $uri_segments[sizeof($uri_segments)-1]) {
array_pop($uri_segments);
}
$uri = implode('/',$uri_segments);
$url = 'http://'.$host.'/'.$uri.'/'.$row_menu['page_link'];
echo $url;

Reading URL in php [duplicate]

This question already has answers here:
Get the full URL in PHP
(27 answers)
Closed 9 years ago.
I want to check the request is come from which URL LIKE --- www.mysite.com or bussiness.mysite.com .How can we check it,as $_SERVER['HTTP_HOST'] gives whole URL .
Thanks .
The thing you want is Subdomain of your url. Use this
$subdomain = array_shift(explode(".",$_SERVER['HTTP_HOST']));
$domain = $_SERVER['HTTP_HOST'];
$path = $_SERVER['SCRIPT_NAME'];
$queryString = $_SERVER['QUERY_STRING'];
$url = "http://" . $domain . $path . "?" . $queryString;
echo "The current URL is: " . $url . "";
visit :http://www.2basetechnologies.com/

Get Request URI PHP

I am struck in getting the URI in my wordpress application and lack of PHP knowledge is making my progress slow.
I have this URL
http://abc.com/my-blog/abc/cde
i need to create a URL something like
http://abc.com/my-blog/added-value/abc/cde
where http://abc.com/my-blog is the URL of my wordpress blog which i can easily get using following method
home_url()
i can use PHP $_SERVER["REQUEST_URI"] to get request URI which will come up as
/my-blog/abc/cde
and than i have no direct way to add value as per my requirement
is there any way to achieve this easily in PHP or Wordpress where i can get following information
Home URL
Rest part of the URL
so that in end i can do following
Home-URL+ custom-value+Rest part of the URL
My point of Confusion
On my local set up $_SERVER["REQUEST_URI"] is giving me /my-blog/abc/cde, where /my-blog is installation directory of wordpress and i can easily skip first level.
On production server its not same as /my-blog will not be part of the URL.
Very briefly:
<?php
$url = "http://abc.com/my-blog/abc/cde";
$parts = parse_url($url);
$path = explode("/", $parts["path"]);
array_splice($path, 2, 0, array("added-part")); //This line does the magic!
echo $parts["scheme"] . "://" . $parts["host"] . implode("/",$path);
OK, so if $addition is the bit you want in the middle and $uri is what you obtain from $_SERVER["REQUEST_URI"] then this..
$addition = "MIDDLEBIT/";
$uri = "/my-blog/abc/cde";
$parts = explode("/",$uri);
$homeurl = $parts[1]."/";
for($i=2;$i<count($parts);$i++){
$resturl .= $parts[$i]."/";
}
echo $homeurl . $addition . $resturl;
Should print:
my-blog/MIDDLEBIT/abc/cde/
You might want to use explode or some other sting function. Some examples below:
$urlBits = explode($_SERVER["REQUEST_URI"]);
//blog address
$blogAddress = $urlBits[0];
//abc
$secondPartOfUri = $urlBits[1];
//cde
$thirdPartOfUri = $urlBits[2];
//all of uri except your blog address
$uri = str_replace("/my-blog/", "", $_SERVER["REQUEST_URI"]);
This is a reliable way to get current url in PHP .
public static function getCurrentUrl($withQuery = true)
{
$protocol = stripos($_SERVER['SERVER_PROTOCOL'], 'https') === false ? 'http' : 'https';
$uri = $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
return $withQuery ? $uri : str_replace('?' . $_SERVER['QUERY_STRING'], '', $uri);
}
You can store the home url in a variable, using wordpress, using get_home_url()
$home_url = get_home_url();
$custom_value = '/SOME_VALUE';
$uri = $_SERVER['REQUEST_URI'];
$new_url = $home_url . $custom_value . $uri;

PHP normalize remote url's [duplicate]

This question already has an answer here:
How do I apply URL normalization rules in PHP?
(1 answer)
Closed 9 years ago.
Is there any quick function that will convert: HtTp://www.ExAmPle.com/blah to http://www.example.com/blah
Basically I want to lower case the case-insensitive parts of a url.
No, you'll have to write code for it on your own.
But you can use parse_url() to split the URL into its parts.
Since you asked for "quick," here's a one-liner that does the job:
$url = 'HtTp://User:Pass#www.ExAmPle.com:80/Blah';
echo preg_replace_callback(
'#(^[a-z]+://)(.+#)?([^/]+)(.*)$#i',
create_function('$m',
'return strtolower($m[1]).$m[2].strtolower($m[3]).$m[4];'),
$url);
Outputs:
http://User:Pass#www.example.com:80/Blah
EDIT/ADD:
I've tested, and this version is about 55% faster than using preg_replace_callback with an anonymous function:
echo preg_replace(
'#(^[a-z]+://)(.+#)?([^/]+)(.*)$#ei',
"strtolower('\\1').'\\2'.strtolower('\\3').'\\4'",
$url);
I believe this class will do what you're looking for http://www.glenscott.co.uk/blog/2011/01/09/normalize-urls-with-php/
Here's a solution, expanding on what #ThiefMaster already mentioned:
DEMO
function urltolower($url){
if (($_url = parse_url($url)) !== false){ // valid url
$newUrl = strtolower($_url['scheme']) . "://";
if ($_url['user'] && $_url['pass'])
$newUrl .= $_url['user'] . ":" . $_url['pass'] . "#";
$newUrl .= strtolower($_url['host']) . $_url['path'];
if ($_url['query'])
$newUrl .= "?" . $_url['query'];
if ($_url['fragment'])
$newUrl .= "#" . $_url['fragment'];
return $newUrl;
}
return $url; // could return false if you'd like
}
Note: Not battle-tested but it should get you going.

Categories