Nedd to read URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mzfp2
Forum Contributor
Posts: 137
Joined: Mon Nov 11, 2002 9:44 am
Location: UK
Contact:

Nedd to read URL

Post 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
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

Post 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.
mzfp2
Forum Contributor
Posts: 137
Joined: Mon Nov 11, 2002 9:44 am
Location: UK
Contact:

Post 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?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

You can use sessions, or cookies, or post to pass things without url-mangling.... I'ld recommend sessions.
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

Post 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?
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

Post 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.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
Post Reply