Page 1 of 1
Nedd to read URL
Posted: Mon Jun 23, 2003 12:50 pm
by mzfp2
Hi all, I want to install a small script which reads the URL the user used to access my site, this way I can implement a feature similar to face party which allows profiles to be seen using a simple web address like
http://www.alljammin.com/admin
which would allow the admin's profile to be seen. So by reading this url into a string, i can simply extract the username after the the
http://www.alljammin.com/ domain and use the remaining string to load the profile.
The alternative seems ridiculous, to create a directory for each user and a index file in each directory which would result in thousands of useless subdirectories.
Many thanks
Posted: Mon Jun 23, 2003 1:12 pm
by Galahad
Maybe it would be better if you had a page like "profile.php" that displayed the users profile. Then you could send someone to
Code: Select all
www.yourpage.com/profile.php?id=123
to view the profile of the user with id 123. That way you just have one page that does it for everyone. Otherwise, I believe you would have to do some url-rewriting at the apache level, and probably would still have to pass the user's name or id to your resulting page.
Posted: Mon Jun 23, 2003 1:16 pm
by mzfp2
yeah that seems much easier, but its not user friendly at all .. so there is no way of reading the url from the address bar, even in JavaScript?
Posted: Mon Jun 23, 2003 1:32 pm
by nielsene
You can use sessions, or cookies, or post to pass things without url-mangling.... I'ld recommend sessions.
Posted: Mon Jun 23, 2003 1:44 pm
by Galahad
You can read the url with $_SERVER['PHP_SELF'], but the problem is that you need a page that actually gets loaded to do that. If a user goes to "
www.yourdomain.com/username/", you need that url to actually get you to a page so that your php script can do something. Like you said, it's a little ridiculous to make individual directories and pages for each user.
The way around that is to make your web server do some redirecting. Apache can do this with
mod_rewrite. It can redirect the page while making the url look the same. I have very little experience actually using mod_rewrite, but I would try something where all attempts to access a subdirectory (let's say "
www.yourdomain.com/subdir/" for example) get redirected to "
www.yourdomain.com/profile.php?dir=subdir". The browser will still show the url as "
www.yourdomain.com/subdir/" (if you set it up that way). Essentially you are doing the same thing as I suggested before, but hiding it from the user.
Personally, I don't see passing an id or name in the url as being that bad, and in my eyes at least, not worth the trouble of getting mod_rewrite going. Why do you see that as not being user friendly?
Posted: Mon Jun 23, 2003 2:02 pm
by Galahad
Sessions are not a bad idea, but they are not necessarily the best way to go. For instance, if you have a page generated from session variables, the user will not be able to bookmark the page and return to it later. Also, making multiple browser windows use different sessions is somewhat problematic. If they use the same session, and you try to view two users profiles at once, the pages can interfere with each other's data. You can pass the session id through the url to make sure the windows use the correct session, but that doesn't seem to be an option, since it would make the url very, very ugly.
Actually making a link to view a profile deal with session variables seems a little clumsy. Maybe there is a better way to do this, but I think you would have to send the link to something like "redirect.php?id=123" and have that page set the session variable to the correct value and immediately bounce you to "profile.php" which uses the session to figure out which profile to display. Unless I'm not seeing something, that seems kind of clumsy, and like I said before, you can't get back to that profile later on (after that session has been destroyed or a different profile has been viewed) without going through something like the redirect.php page.
Seriously, I think your users can handle a slightly ugly url.
Posted: Mon Jun 23, 2003 2:07 pm
by nielsene
No, ugly urls are bad. They are a horrible short-cut that too many developers use. They are appropriate in a prototype system, but in a production system they look bad.
Other options, look at some of the articles at alistapart or other design sites about using "simulated" urls. Ie
a site/tool/category/title url that gets mapped internally to site/view_tool.php?category=category?title=title type thing using PHP, not rewrite rules. You basically set Apache up to run PHP scripts without the .php extension and to travese backward up the directory tree if the file is not found.