This question already has answers here:
Get the full URL in PHP
(27 answers)
Closed 5 years ago.
I am starting to build my own MVC using PHP and I am trying to get the current URL in order to create a routing system. I am following a video and the code is as follows. However, it isn't working.
<?php
// phpinfo();
echo "15";
$url = $_GET['url'];
// echo $url;
You are mistaking $_SERVER['REQUEST_URI'] for $_GET['url']
$_GET['URL'] is a get statment .. Meaning you need to pass a variable through the URL IE http://example.com?URL=myurl
To get the current URL .. Simply use echo $_SERVER['REQUEST_URI'];
$_GET will get you the get parameters, not the entire url. Use $_SERVER['REQUEST_URI'] for that.
Related
This question already has answers here:
How do I make a redirect in PHP?
(34 answers)
Closed 6 months ago.
Hello, I want to make a panel so that we can change the redirection link of the page,
in the header(); .
I want that the header($result); redirect to "amazon.com" the official site (for example) and not http://localhost/amazon.com
($result which is amazon.com obviously)
Is it possible ?
I have already tried with javascript :
<script type="text/javascript">
window.location.href = '<?php echo $result; ?>';
</script>
But it also returns me
"http://localhost/amazon.com"
And here is the php code
header("Location: .$result");
exit;
Thanks in advance for your help
Your URL is missing the scheme (https:// or http://) or the marker to indicate that it is a scheme-relative URL (//).
amazon.com is therefore treated as a relative path and not as a hostname.
You need https://amazon.com
This question already has answers here:
Get entire URL, including query string and anchor
(8 answers)
Closed 5 years ago.
Please help me out, if i have a URL like this
http://www.example.com/dashboard.php#settings
How do i get it in full cause. when I use:
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
I get http://www.example.com/dashboard.php it excludes the # and the text after it, how do I go about getting everything in full like this http://www.example.com/dashboard.php#settings.
Url fragments are never sent to server side you have to use get or post and send the value to server side in order to perform some actions or you can use your hashtag but also some client-side javascript to send the hashtag value as post or get parameter to the server side.
parse_url() can parse the URL with fragments but it is useless in this case since the fragment is never sent to the server side.
if(window.location.hash) {
// fragment exists send request to server side as get or post
} else {
// fragment does't exists
}
This question already has answers here:
Get fragment (value after hash '#') from a URL [closed]
(10 answers)
Closed 6 years ago.
I am very new to PHP and would like to build a web app for my school.
I have got the oauth down, and my redirect URl is
127.0.0.1/authorize
This results in, when succesfulyl authenticated, 127.0.0.1/authorize/#access_token=thenmycodehere
Now, being new to PHP, I cannot figure out how to get the access_token variable.
If the # was a ?, then I could do it.
Is it possible?
exec("python getlinks.py " . $_GET["access_token"]);
You cannot.
Browser won't even send a request with a hash part.
Only way is just send this access_token using some clientside js code.
To somebody saying it's possible - I want to prove that it's not!
Here is the prove:
1) create hash.php file and put this contents:
<?php
echo "SERVER<br/>";
var_dump($_SERVER);
echo "<hr/>";
echo "REQUEST<br/>";
var_dump($_REQUEST);
2) go to link and check: http://num8er.me/hash.php#access_token=blablabla
This question already has answers here:
How to get full url of a article by it's ID in joomla?
(3 answers)
Closed 8 years ago.
I'm using joomla 2.5. I'm trying to read URL variable OR full URL in Joomla component customization.
I'm able to read URL like:
http://xyz/index.php?option=com_xyz&format=raw&tmpl=component&lang=en
But I would like to read URL which is on browser (with alias):
http://xyz/en/feedback?ID=123456&Email=abc#hotmail.com
feedback is a Joomla alias. I'm redirecting that URL with variable from another PHP script.
If I try:
$_SERVER['HTTP_REFERER']; // not working in IE.
$_SERVER['QUERY_STRING'] //return com_xyz&format=raw&tmpl=component&lang=en
JRequest::getVar('Email'); // return NULL but works if try getVar('lang');
Is there any other solution or better way?
Try using the following. Might be what you're looking for:
$url = JUri::current();
echo $url;
Using Javascript, you could use this:
document.location.href
This question already has answers here:
Get the full URL in PHP
(27 answers)
Closed 8 years ago.
In PHP, how can I get the URL of the current page? Preferably just the parts after http://domain.example.
$_SERVER['REQUEST_URI']
For more details on what info is available in the $_SERVER array, see the PHP manual page for it.
If you also need the query string (the bit after the ? in a URL), that part is in this variable:
$_SERVER['QUERY_STRING']
If you want just the parts of URL after http://domain.example, try this:
<?php echo $_SERVER['REQUEST_URI']; ?>
If the current URL was http://domain.example/some-slug/some-id, echo will return only /some-slug/some-id.
If you want the full URL, try this:
<?php echo 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>
$uri = $_SERVER['REQUEST_URI'];
This will give you the requested directory and file name. If you use mod_rewrite, this is extremely useful because it tells you what page the user was looking at.
If you need the actual file name, you might want to try either $_SERVER['PHP_SELF'], the magic constant __FILE__, or $_SERVER['SCRIPT_FILENAME']. The latter 2 give you the complete path (from the root of the server), rather than just the root of your website. They are useful for includes and such.
$_SERVER['PHP_SELF'] gives you the file name relative to the root of the website.
$relative_path = $_SERVER['PHP_SELF'];
$complete_path = __FILE__;
$complete_path = $_SERVER['SCRIPT_FILENAME'];
The other answers are correct. However, a quick note: if you're looking to grab the stuff after the ? in a URI, you should use the $_GET[] array.
You can use $_SERVER['HTTP_REFERER'] this will give you whole URL for example:
suppose you want to get url of site name www.example.com then $_SERVER['HTTP_REFERER'] will give you https://www.example.com