tags:

views:

261

answers:

4

Hello,

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!

+3  A: 
<?php
  echo $_GET['oauth_token'];
?>
Ignacio Vazquez-Abrams
+2  A: 

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'];
Will Vousden
+3  A: 

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']);
zombat
Or you could just validate $_GET['oauth'] which looks like it should be a ^[0-9]+$
Bobby Jack
Yes, you could. I wouldn't trust them to not change the OAuth specs in the future to include hashes, but you could always read the docs to determine what it should be at the moment.
zombat
+2  A: 

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.

kemp