PHP not recognizing GET variables with hash in the URL - php

I have a URL like:
http://www.example.com/page.php#tabname
The hash will automatically open a specific tab on the page.
I need to work with a _GET variable, and the URL is like:
http://www.example.com/page.php#tabname?color=red
Then on the page, I have:
echo $_GET['red'];
...but I am getting an undefined index error. How do I get PHP to recognize the variable?

Anything after the hash is not sent to the server. Regardless, you should probably format your url so you send the GET parameters correctly.
http://www.example.com/page.php#tabname?color=red
should be
http://www.example.com/page.php?color=red#tabname

You need to put the query string before the hash:
http://www.example.com/page.php?color=red#tabname

Related

httprequest getting back PHP functions and variables

How does one access and receive back a variable from a PHP file with a HTTP Request?
I have gotten a HTTP Request to connect to my .PHP file. What I wanted to do next is for example reference that I want to receive $testvariable = 1; back but I have no idea where to begin. The HTTP parameters don't really let me reference this $testvariable directly.
What if the PHP file has simply one function and at the end of it it does a return $testvariable;? Would HTTP receive back this one variable? What if I need more than one. Maybe try and get the PHP to place parameters in the URL and the HTTP reads those parameters in the url? Maybe these "headers" are key to this...
I figured it out. HTTP Request gets back ALL that is on your HTML page. It seems like it ignores <html><body> tags but everything else in the body gets taken back at your response. Even if your answer is " 1 " but you have a single space around it your response is 1 including blank spaces. You need a clean "1".
Passing variables works by using the contents field. You place your values and assign them to a string like number= that then gets posted on the page. When a page is loaded, it can look for a variable under the same name like $test = $_POST['number']; and take the value within that variable and use it further where it needs to on your page.

How to access url after # in php

I am trying to pass some parameters after # in the url like http://developer.rohitkhatri.com/test.php#embed=sdkhfjshdkfhhjk, But I don't know how to access it, I tried many solution from the stackoverflow, here are some examples what I've tried:
$_SERVER['REQUEST_URI'] gives me /test.php
$_SERVER['QUERY_STRING'] gives empty string
$_SERVER['HTTP_REFERER'] gives empty string
also tried printing the whole $_SERVER array but I did not find anything useful.
Any help is appreciated.
Well, there's no way to achieve this, because the part you are trying to access using the php, never goes to the server, what you can do is, just grab the part using the javascript and send it the the server.
Like there can be a middle page, which will redirect to the final url, and while redirecting, It can grab the part after # and send it using ajax.
The browser doesn't send anything that comes after the hash(#) to the server because it is resolved within the browser. You can try by mentioned code.
$hash = '<script>document.write(document.location.hash)</script>';
echo $hash;
output :
//#embed=sdkhfjshdkfhhjk

PHP header_redirect with multiple variables

I am writing a PHP script that will redirect based on a link like this:
header_redirect($_GET['redirect']);
And the URL being:
https://www.url.com/index.php?change_language=english&redirect=other_page.php?category=1234&limit=24
As you can see, the actual page is
https://www.url.com/index.php?change_language=english
and the redirect is then to another page with multiple variables like:
&redirect=other_page.php?category=1234&limit=24
However, when I run the link above, I only get redirected to "other_page.php" and the additional variables are lost.
How would I achieve this?
You can solve this problem using some encryption and decryption trick, I have used base64_encode and base64_decode() function to solve your problem.
Step1: in your html page
<?php $redirectUrl = base64_encode('other_page.php?category=1234&limit=24'); ?>
Link1
Step 2: in your header_redirect() function, you can use base64_decode() function decode the redirect stings and get the expected one.
function header_redirect($redirect){
$redirect = base64_decode($redirect); // you can get the expected redirected url with query string
//your redirect script
}
Depends on what you're trying to do.
Option 1: Use http_build_query.
Try:
$target=$_GET['redirect'];
unset($_GET['redirect']);
$query_str=http_build_query($_GET);
header_redirect($target.'?'.$query_str);
If your starting URL is this:
https://www.url.com/index.php?change_language=english&redirect=other_page.php&category=1234&limit=24
You would then be redirected to:
https://www.url.com/other_page.php?change_language=english&category=1234&limit=24
Option 2: Use rawurlencode and rawurldecode.
However, if your goal is to be redirected to whatever you have stored in $_GET['redirect'] (and ignore any other variables in the URL), then you would need to encode the other_page.php&category=1234&limit=24 bit before you put it into your starting URL. This will effectively escape the special characters and allow you to simply call header_redirect(rawurldecode($_GET['redirect']));.
Say your starting URL is then:
https://www.url.com/index.php?change_language=english&redirect=other_page.php%3Fcategory%3D1234%26limit%3D24
You would then be redirected to:
https://www.url.com/other_page.php?category=1234&limit=24

Use link url upon click in PHP

I want to use the URL of the link that was clicked in a VB.net program. How can I take the url from my browser url bar and use it in my PHP program?
Example:
VB.net - click link then open using a web browser
url: www.something.com/id=^%$##&var2=13lfhd3f4gt
PHP - put the link in a variable or something so that I can use explode command to get the id and var2 from the URL itself
I need those variables to output a certain value from my database.
This question has the answer that you are looking for, for getting the url in PHP:
Get the full URL in PHP
Though for getting id and var2, it would be simpler to just use the $_GET variable in php. Then you don't have to explode the url and process it. Just change the '/' after 'com' to a ? like:
www.something.com?id=^%$##&var2=13lfhd3f4gt
You can do this using eg. $_SERVER global variable. Please refer to the manual

How to display URL parameters in PHP script?

I want to redirect my browser to a PHP page such that when the page loads, it will display to the user a substring of the current URL.
For example, let's say I have a page called substring.php.
My browser forwards me to:
http://www.example.com/substring.php?oauth_token=123456
Is it possible to write some PHP code that will then display to the user, "123456"?
If so, can anyone help me on how to do this?
Thanks!
All the query parameters in the URL will be inside the superglobal $_GET array, so you could simply do this:
echo $_GET['oauth_token'];
BE forewarned that if you're going to output anything that comes in from a URL (ie. user input), you should make sure to sanitize it properly for output. In this case, htmlspecialchars() would be prudent:
echo htmlspecialchars($_GET['oauth_token']);
<?php
echo $_GET['oauth_token'];
?>
Can't you just use the $_GET superglobal? It stores the contents of the query string part of the URI as an associative array:
echo $_GET['oauth_token'];
You can retrieve the value of oauth_token via the $_GET superglobal array:
echo $_GET['oauth_token'];
Of course you should use caution when outputting data you get as input from a user, but that's how it works in short.

Categories