User ID in URL

I want people to be able to share there public pages with other people, but I have no skill how to do this. I’ll explain this in parts.

First, I want people friends to be able to access the user public page by going “http://domainname.com/USERNAME” or by going “http://domainname.com/user.php?id=12345”. I’d preferably use the first option, but I know that may be too complicated.

Second, I have no clue how to make this code.

Could someone please help me or guide me. I know that isn’t what the forums are for, but I’d greatly appreciate it.

To sum this up, could someone please help me make a public page for a user with either a “http://domainname.com/USERNAME” URL (first choice) or a “http://domainname.com/user.php?id=12345” URL? Thanks for the help!

I posted the reg. code in the Regex Help thread.

You can write URLs like

somesite/user.php/some_identifier

without using URL rewriting. The $_SERVER[“PATH_INFO”] contains the stuff that comes after “user.php” (your script), in this case it contains “/some_identifier” and you can parse that and do what you want with it, just like you would with a $_GET variable.

Now, in some really fancy apps, you might have

somesite/controller.php/user/{user_id}

in this case, “controller.php” is a “superscript” that renders all (or many) of the pages on your site, and it’s smart enough to see that the PATH_INFO starts with /user/, and pass the job to some component that reads the {user_id} and then draws the right page. There all kinds of architectural advantages of this, and its the way that modern ‘MVC’ webapps work.

Now, if you don’t like the “.php” there are a bunch of ways to make that go away.

@oknow,

 $_SESSION variables create nightmares for people who don't [I]really[/I] understand what they do.  You'd do much better as a PHP beginner if you pretended that they don't exist,  [B]trust me[/B].

 In your case,  just use good 'old $_GET instead of $_SESSION.  $_GET captures any variables that are defined in the URL and does the job nice and easy.

In order to understand the first choice you need to consider the third choice:

http://domainname.com/user.php?username=someusername

So it is like second choice only instead if id you use username as the identifier. You then select your data from database using WHERE username='$_GET[‘username’]

username is usually just as unique as the id, so it will work.

Then you can make the first choice http://domainname.com/USERNAME and then
just use rewrite rule to redirect to the third choice

use mod_rewrite

This came up from google feeling lucky, should help you get the urls sorted.

To sum this up, could someone please help me make a public page for a user with either a “http://domainname.com/USERNAME” URL (first choice) or a “http://domainname.com/user.php?id=12345” URL? Thanks for the help!

So the url bit is optional, what do you want help with here? If you have absolutely no idea what to do then google for some basic turorials. Otherwise, have a go, and come back here with some code when you get stuck.

I’ve been trying to do this exact thing as well, and it’s surprisingly hard to google for.

I want to have a user page that displays different data depending on the username.

I thought I had it figured out with this code at the top of my main.php page.


<?php
session_start();
if (isset($_SESSION['username'])) {
$user = $_SESSION['username'];
}
else
$user = '';
?>

and then for the link going to my user.php page I did this:


<?php echo '<a href="user.php?user=' . $user . '">' ?>User Page</a>

which works great for someone who is logged in, but 1) I think it’s just the incorrect way to go about it and 2) it is completely useless for someone who ISN’T logged in trying to view someones user page.

maybe we can both get help with this :slight_smile:

Well said :slight_smile:

I’m probably going to try the second option before the first one. Thanks for all your help!

I replied to the regex thread.

It’s important that you realize users are not redirected but just ‘rewritten’ as if they had requested a different URL.

To get the data from the database, check out the manual on mysql_query() and related functions.

I have the first redirect code.

RewriteEngine  on
RewriteRule ^/social/([A-Za-z]+,[0-9]+)$ /user.php?id=$1

See my Regex Help thread for more on that.

Can someone help me with getting the information from the database? The not logged in user will enter [I]http://domainname.com/USERNAME[/I], then they will be redirected to [I]http://domainname.com/user.php?user=USERNAME[/I]. How do I get the information for USERNAME? I don’t understand how I would do that.

Thanks for the help!

:slight_smile: