...

View Full Version : Variable from URL



FuZion
06-09-2007, 05:35 AM
Ok lets say I have a script under post/index.php. And I need a variable from the URL but I really would like to find away to not use ?id=xxx. What I'd like to do is something like www.mysite.com/posts/id.

Is there anyway to retrieve the var from this?

Thanks a lot!

FuZion

whizard
06-09-2007, 06:58 AM
Check out .htaccess's mod_rewrite abilities

http://www.workingwith.me.uk/articles/scripting/mod_rewrite
http://corz.org/serv/tricks/htaccess2.php

HTH
Dan

Coastal Web
06-09-2007, 07:00 AM
Ok lets say I have a script under post/index.php. And I need a variable from the URL but I really would like to find away to not use ?id=xxx. What I'd like to do is something like www.mysite.com/posts/id.

Is there anyway to retrieve the var from this?

Thanks a lot!

FuZion


Well modrewrite is how it gets done.
---
Here's an easy way to do it for beginners and learn the ropes of mod rewrite.

in your .htaccess file put this:

RewriteEngine On
RewriteRule ^(PHPFILE1)(/.*)?$ $1.php

where PHPFILE1 is the name of the php file that you'd like to do this with (you can add as many as you like.
example:

RewriteEngine On
RewriteRule ^(PHPFILE1|PHPFILE2|PHPFILE3|PHPFILE4)(/.*)?$ $1.php

-------------

once that's done..
in the PHPFILE1.php place this bit of code into it:

<?php
$urlArray = explode("/",$_SERVER["REQUEST_URI"]);

$page_url = $urlArray[count($urlArray)-1];
$page_id = $urlArray[count($urlArray)-2];

echo <<<end
Page URL: $page_url
<br />
Page id: $page_id
end;
?>
Finally, visit this url in your browser:


http://www.YOURSITE.com/FILE1PHP/1234/fake-file-name.html

and the script will output:

Page URL: fake-file-name.html
Page id: 1234

Again, this isn't the best way to do it; but it's the easiest for a beginner with mod-rewrite to understand and use.

Best,

GSimpson
06-09-2007, 01:14 PM
If you don't mind me asking, why are you hiding the id? not like it's a password. And if your logged in, (just stereotypically) why don't you have a field that has to match current cookie setting. Just to save the effort.

FuZion
06-09-2007, 04:50 PM
If you don't mind me asking, why are you hiding the id? not like it's a password. And if your logged in, (just stereotypically) why don't you have a field that has to match current cookie setting. Just to save the effort.

Well, I have a site that will receive a lot of "posts" if you will, when the user posts something they will be given a unique id that they will have to remember. And in order for them to visit it I'd like for them to be able to go to www.mysite.com/posts/ID or something like that.

Thanks a bunch for your help guys!

GSimpson
06-10-2007, 12:17 AM
why not just do it like this forum, I mean it receives at least 100 posts a day and has been built to go forever (speaking theoretically). and remembering your id is ok, but if it just gonna be entered in the url, why not just do it after ?id=

digital-ether
06-10-2007, 12:48 AM
why not just do it like this forum, I mean it receives at least 100 posts a day and has been built to go forever (speaking theoretically). and remembering your id is ok, but if it just gonna be entered in the url, why not just do it after ?id=

Not everyones a coder.

example.com?id=x looks very foreign compared to example.com/id/

example.com/id/ is considered a search engine friendly (SEF) url - though it would be more SEF if you placed the 'title' instead of the 'id' in the URL.

This becomes even more apparent the more url params you have:

example.com/php/what-is-php/

compared to:

example.com?f=1&p=2

who can remember that? who wants to add that to their blog when it doesn't have any keywords reflecting their blog post.. etc etc...

automated services can easily classify/tag/aggregate/index/bla..bla your content into their own system if you give them hints..

the list goes on...

firepages
06-10-2007, 04:20 AM
FYI, there is another option and that is to use the $_SERVER['PATH_INFO']

e.g. with the url domain.com/index.php/article/nice-day
echo $_SERVER['PATH_INFO']; outputs '/article/nice_day'

you can then use explode('/',$_SERVER['PATH_INFO']) etc to map those into variables.

I use this method myself though it is not without gotchya's the main one being that relative paths will now be wrong , so to include a file in the same directory as index.php you will need to prepend ../../ to get at that file.
Thats not an issue for me as all my pages use absolute paths but it is worth noting.

The other issue is the predetermined variable order but thats the same with mod-rewrite as well.

If your server supports force-type you can also lose the '.php' part of the usl

GSimpson
06-10-2007, 12:17 PM
Ok so not every ones a coder, why not just have a page ie somedomain.com/index.php and have a form ENTER YOUR ID: than have it goto http://somedomain.com/posts/?id=$_POST['input'], for example.

digital-ether
06-11-2007, 10:05 AM
Ok so not every ones a coder, why not just have a page ie somedomain.com/index.php and have a form ENTER YOUR ID: than have it goto http://somedomain.com/posts/?id=$_POST['input'], for example.

thats called google. Works best with names instead of ids... :)
getting off topic here...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum