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;
Related
For instance, I need to add http:// to submitted url like :
//www.mydomain.com/images/icon-32.png (double slash at the beginning)
or
www.mydomain.com/images/icon-32.png
Any idea?
This seems to work fine for your problem.
$url = "//mydomain.com/images/icon-32.png";
// or $url = "mydomain.com/images/icon-32.png";
// Checking if string starts with '//'
if(strpos($url, "//")===0){
$url = "http:" . $url;
}else{
// Checking if string doesn't already start with 'http://'
if(!strpos($url, "http://")===0 || strpos($url, "http://")===false)
$url = "http://" . $url;
}
echo $url;
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;
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;
How can I get my full current url in php but minus all querystrings?
Example
echo 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
Would echo something like assuming query strings were in place...
http://www.example.com/example?tab=foo&dslip=yes
How can I get the same as above but cut off all the query strings?
How is this done in php.
Thanks.
PHP_SELF is what you need I believe.
echo('http' . ((empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === 'off') ? '' : 's') . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF']);
Or the __FILE__ constant, depending on your exact configuration and situation.
Try this
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$part = explode('?',$url);
echo $part[0];
I think the solution is this:
echo 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'];
the $_SERVER['SCRIPT_NAME'] variable returns path of your script without the query string like you want.
if you like to see all variables available in php just try this
phpinfo();
Best regards
How can I get the full URL, like http://www.domain.com/page.php?id=someid&page=1, not just http://www.domain.com/page.php?
$_SERVER['QUERY_STRING'] has the query string portion of the URL.
A quick answer:
$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING'];