PHP parse_url not working for current URL [duplicate] - php

This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 8 years ago.
It works
$url = parse_url('http://yabadaba.com/brand#asos');
echo $url['fragment'];
But it doesn't work
$url = parse_url($_SERVER['REQUEST_URI']);
echo $url['fragment'];
What is wrong? :(

That is because the parameter preceded by # will not reach the server-side script. You can get it only by Javascript.
Why does it work on the first case ?
That is because you are hard-coding it in the parse_url function.

Related

How to get the full URL of the current page using PHP [duplicate]

This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 8 years ago.
What is the "less code needed" way to get parameters from an URL query string which is formatted like the following?
My current url
www.mysite.com/category/subcategory/#myqueryhash
I put this code
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
It returns only www.mysite.com/category/subcategory/
Output should be :
www.mysite.com/category/subcategory/#myqueryhash
You can use this for HTTP request
<?php $current_url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>
You can use this for HTTPS request
<?php $current_url="https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>
You can use this for HTTP/HTTPS request
<?php $current_url="//".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>

Remove url from string using PHP [duplicate]

This question already has answers here:
Get path from URL
(2 answers)
Closed 8 years ago.
Can anyone please tell me how to remove url from a string using PHP
I have this string
src="http://cdn1.vox-cdn.com/uploads/chorus_image/image/34918177/layering__1_.0_standard_90.0.png"
Desired Output:
src="/uploads/chorus_image/image/34918177/layering__1_.0_standard_90.0.png"
You can use the parse_url() buil-in php function for this.
For example:
<?php
$url = 'http://cdn1.vox-cdn.com/uploads/chorus_image/image/34918177/layering__1_.0_standard_90.0.png';
$parsed_url = parse_url($url);
var_dump($parsed_url);
?>
Output:
So, after the url is parsed, the bit you want is found on $parsed_url['path']. Hope this helps.

PHP Regular expression - forget everything after? [duplicate]

This question already has answers here:
Beautiful way to remove GET-variables with PHP?
(12 answers)
How to remove content from url after question mark. preg_match or preg_replace?
(2 answers)
Closed 8 years ago.
I have this url: http:www.blabla.com/x/x/x/x?username=testuser
I need a string to read this url, but forget everything and including the ? mark.
So it becomes this: http:www.blabla.com/x/x/x/x
The reason for this is because I am making this variable:
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
And this code:
if($host == "http:www.blabla.com/x/x/x/x") {
echo "lul";
}
But right now, the URL changes depending on what user is on, and it has to execute the echo no matter what user is on.
So I read some reges and preg_match etc. and I just wanted to hear your opinions or advice. How would I accomblish this the best? thanks!
This is too trivial of a task for regex.
$host = $_SERVER['SERVER_NAME'] . explode("?", $_SERVER['REQUEST_URI'], 2)[0];
(Note: this assumes you're up-to-date, or at least using PHP 5.4, for the dereference to work without a temporary variable)
Or if you must omit the get / request section just explode ? and use $host[0]

How can I get the full URL in PHP, not just part of it? [duplicate]

This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Get the full URL in PHP
(27 answers)
Closed 9 years ago.
This is my current URL structure
http://wp.raddyx.in/consultant-dietitians/staff-single/#Fiona%20Brown
I am using echo $url=$_SERVER['REQUEST_URI'];
However it displays /consultant-dietitians/staff-single/ only, where I want to fetch the full URL.
Use $_SERVER["SERVER_NAME"] to get the host name wp.raddyx.in
Use $_SERVER["HTTPS"] to check for http vs https.
You might need $_SERVER["SERVER_PORT"] too and some other misc things that can appear in a URL (like PHP_AUTH_USER)
You can not get the hash part of the URL (#Fiona%20Brown) since hashes are client-side only. They are not sent to the server.
Relevant manual page: http://php.net/manual/en/reserved.variables.server.php
Use $_SERVER[HTTP_HOST]:
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
echo $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

PHP get URL content [duplicate]

This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 9 years ago.
I have a URL
http://localhost/ifocus/v3/services.php#nav-section2
I want to get hashtags from URL, EX : if the URL contains "#" so capture it via PHP
nav-section2
When i do
echo $_SERVER['REQUEST_URI'];
// Output
/ifocus/v3/services.php
You can't detect the hash parameter of the URL in PHP, you would need to use JavaScript.
You could make a jquery call to php which passes the hash data (window.location.hash) to a PHP script as a URL parameter and return TRUE/FALSE though.
You can not capture this content for that you need js(client side script)

Categories