1

I have all internal links as a MVC scheme without .php extensions. Example: http://examplesiste.com/login will detect "login" using this algorithm:

$page = substr($_SERVER['REQUEST_URI'], 1);

and load footer, header and detected "login.php" between. Though the final link will look like this: http://examplesidte.com/login (without .php)

How should I pass GET variables? with php extenstion it's quite obvious - http://examplesidte.com/login.php?id=abc666 but , of course http://examplesiste.com/login?id=abc666 won't work any suggestions?

3
  • How I generally do this is parsing the URI string Commented Jan 17, 2015 at 5:07
  • so , basically, you mena- just to write it this way: examplesidte.com/login/abc666 ? I emean - yeah - that will do the trick! but - does it mean that GET variables are not posisble to pass at all? and - thanks, man! Commented Jan 17, 2015 at 5:10
  • I haven't done a lot of work with this in particular. In some instances I just setup a redirect to append any data in a query string, but in a case I was working for over the summer it was just a lot faster to parse the URI string. Since I'm not sure about what this will entail, it is hard to say. Any reason why you got rid of the .php? Commented Jan 17, 2015 at 5:15

2 Answers 2

3

There are many techniques to do it
But here's a quick and simple one you can use in your projects:

All you do — embed parameters into the URL itself, like: /login/id/36/

// first, exploding the URI, getting its parts
$uri = explode('/', $_SERVER['REQUEST_URI']);

// now we have [ '',  'login', 'id', 36' ]
$page = $uri[1]; // 'login' here

// now, finding the parameters
for($i=2; $i < count($uri)-2; $i+=2) $_GET[$uri[$i]] = $uri[$i+1]

print_r( $_GET );
// This will get you: [ 'id' => 36 ]

Viola! Use the resulting $_GET as you usually do!
You can embed as many parameters as you wish: /login/id/36/act/delete/

Sign up to request clarification or add additional context in comments.

3 Comments

glad to help! I used this technique a lot in my early frameworks. Just make sure you escape the slash in text values of parameters, as they will break the chain :)
I usually add in a quick check to make sure that the exploded uri is even before I start looping through. Otherwise you may get some misassigned variables, which can get screwy.
@Jhecht, he may perform this check, or may not. The script won't give any errors though. Better practice is to check all the parameters variables after this. I assume checking all parameters before performing any action is what we all do, right?
0

I think you can hide .php extension in your project with .htaccess file. if you have a file with name ".htaccess" in your project root, just add these lines to it:

RewriteEngine On

#Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]

#Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]

#Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

And if you have not httaccess file before, make a file with this name (".htaccess") and put it in the project root folder.

1 Comment

thanks Hamed, but this is not what I was looking for. Thank you for the comment though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.