Log in

View Full Version : Profile page help.



Jeffreyv1987
10-04-2010, 05:44 PM
I'm making a members page on my website for registered users

i know how to do something like profile.php?member={username} to display there information in the profile page.
and I know there's a way to just put profile.php?{username} but am having a little difficult trying to make it work..

It's probably a simple thing to do but i can't seem to get it to work.

djr33
10-04-2010, 05:54 PM
There are a number of ways you can do this.

1. mod_rewrite: take all incoming requests and actually serve a different page. You'd use regular expressions (code-matching symbols) to transform the request. That's complex and powerful, but you probably don't need it here. You could do profile/{username} with that and skip the rest entirely, though. That's the "cleanest" way but it's also hard to setup.

2. If you will only have this variable, you can take the query string directly. $_SERVER['QUERY_STRING'] contains all of the information from the request URI that comes after the '?'. In your example, it would be {username}. But that will become complex if you have other variables in there too. You could still get this as a string and split it at the first '&', but that might be confusing if the order ever changes.

3. You could check to see isset($_GET['{username}']), but you would need to do this for every possible username. It's very inefficient unless you have only a few members, such as a small group site-- if you are just displaying profiles for 3 people (for example) and they don't change, this method might be the easiest. Otherwise, it wouldn't make much sense.

4. Another interesting method that I recently discovered is what's called PATH_INFO. It's a string that comes after the filename but before the query string.
path/to/mypage.php/PATH_INFO?QUERY_STRING
That might be a nice way for you to separate the username variable and the other query string variables. You'd use this format:
profile.php/{username}?othervar=value
(The final slash, after {username} is optional if you prefer that.)
You would retrieve this information using the variable $_SERVER['PATH_INFO'];
This would also work of course if there is no additional query string:
profile.php/{username}
Note that this variable is not always set: on some servers, at least, it only has a value if there is some value like that in the URL. Use isset() to confirm this before checking it.
(I also know this is not a default variable in PHP, and it may not exist on some servers. I think it will on most though. Test it to be sure.)

traq
10-05-2010, 02:33 PM
PATH_INFO is a great variable. If you have trouble with it, check to see what version of PHP your server runs (many still default to php4) and switch it to php5 if necessary (though you may need to call you host to do so).