John Davidson

php - Retrieve parameter that without prarameter name

0 comments
Message:


I have a registration and activation code that I am updating a bit, after the user registers it sends them a email to activate.


Link emailed


/user/activate/$hashed_confirm_code

which generates


http://example.com/user/activate/0dbc0aef3d4d9d3e67ee4bf6c

I went to http://www.visiospark.com/mod-rewrite-rule-generator/ and did a mod rewrite changed the email link accordingly.


rewrite is on


RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]

RewriteCond %{QUERY_STRING} (^|&)hash_token=($|&)
RewriteRule ^user/activate$ /user/activate/?&%{QUERY_STRING}

if i click the link it produces the following in the browser


http://example.com/user/activate/0dbc0aef3d4d9d3e67ee4bf6c


however it just gives a 500 error, the basics of my activation code function is


function activate_user(){

if (($_SERVER['REQUEST_METHOD'] === "GET") && isset($_SERVER['QUERY_STRING'])) {

$confirm_code = $_SERVER['QUERY_STRING'];

//do stuff


}
}

now if i change the link in the browser


example.com/user/activate/0dbc0aef3d4d9d3e67ee4bf6c93c

to this


example.com/user/activate?0dbc0aef3d4d9d3e67ee4bf6c

it works, so if i remove the forward slash and add the question mark it works



My Answer:
To retrieve the parameter without the parameter name in PHP, you can use the $_SERVER['REQUEST_URI'] variable and then parse the URL to extract the parameter value. Here is an example code snippet to achieve this:

php
// Get the current request URI
$request_uri = $_SERVER['REQUEST_URI'];

// Extract the parameter value from the URI
$parts = explode('/', $request_uri);
$hashed_confirm_code = end($parts);

// Use the $hashed_confirm_code variable in your code
echo "Hashed Confirm Code: " . $hashed_confirm_code;


In this code snippet, we first get the current request URI using $_SERVER['REQUEST_URI']. Then, we split the URI by '/' and extract the last part, which is the parameter value we are interested in. Finally, we store this value in the $hashed_confirm_code variable for further use in your code.

Rate this post

5 of 5 based on 5874 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us