GET method in php clean URL

3.2k Views Asked by At

I have successfully created clean url for my project.. now what i need to do is add variables to URL..

localhost/user1/file?action=add
localhost/user2/file2?action=delete

where user and file are already rewritten i dont want it to be like localhost/user/file/add because localhost/user/folder/file will be mistaken to to the action parameter.. please help

3

There are 3 best solutions below

1
On BEST ANSWER

Try using the ampersand instead of question mark:

localhost/user2/file2&action=delete

In your htaccess, the rewrite rule might look something like this:

RewriteRule ^user([0-9]+)/file([0-9]+)$ /page\.php?user=$1&file=$2

As you can see, the question mark is already there even though it is masked in the address bar. Appending another variable to the query string would require the ampersand for successful concatenation.

2
On

You can read GET variables in PHP by accessing the global $_GET array:

http://php.net/manual/en/reserved.variables.get.php

In your example, the php file that is used for handling files would be able to read in:

echo $_GET['action']; // 'add' or 'delete'
0
On

You need to get the url and start parsing the url from the question mark. I would save the contents then to an array, so that you've got a key and a value.

$uri = $_SERVER["REQUEST_URI"];

$questionMark = explode('?',$uri);

$questionMark[1] is the action=delete then. There are probably better ways then using explode() method here, but I just wanted to show how you get the string.