How to Give Each Registered User Their Own Url Using PHP

Custom URL for each user in PHP

You can implement this using the apache mod_rewrite.

Make a rewrite rule for something like:

^/users/($1)    /users.php?userid=$1

In user.php file read the userid parameter, and return the page corresponding to given user.

As for racking from which user someone registered/logged-in to your site, you can keep a session value, such as the referencing userid, and when the new user registers, write to your db who referred him to your site.

Custom urls - giving each user a url

I really can't justify using htaccess for something like this. I only use htaccess to route everything through one php file (my root index.php) and let php sort out how to handle the url. For example, you could do this:

$uri = trim($_SERVER['REQUEST_URI'], '/');
$pieces = explode('/', $uri);
$username = $pieces[0];

Then do something with $username.

The way I parse and use my url's is a bit more complicated than this, but it wouldn't be relevant to your question. Just make sure whatever you do is able to account for any possible query strings, etc.

mod-rewrite is not great for performance, so I wouldn't abuse it.

Updated based on your comments.

Here's just a slight expansion on my original code sample:

//.htaccess - route all requests through index.php
RewriteCond %{REQUEST_URI} !\.(png|jpe?g|gif|css|js|html)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [L]

and this is an example of what you could do in index.php:

$pieces = preg_split('-/-', $_SERVER['REQUEST_URI'], NULL, PREG_SPLIT_NO_EMPTY);
$username = $pieces[0];

include "users/{$username}.php";

Now you could visit mysite.com/myUserNameHere, the request goes to index.php which parses out the username and includes a file for that user.

That takes care of the routing just like you asked. I said that my routing is more complicated, but my use is a lot more complicated, so it isn't relevant. I only warned that my code here doesn't account for a url with a query string attached, like "mysite.com/someUser/?foo=bar". It's a simple process to parse the url without the query string and you should be able to handle that. If you need further assistance parsing the url, then make a post asking about that.

Automatically Create Custom Page (or URL) for New Site User - PHP

There is little work with apache's .htaccess - most of the logic is in the php itself. Apache just redirects every request to an chosen script file.

I assume you use apache wid mod_rewrite. If that's the case you should add this to .htaccess file:

# check if mod_rewrite is present
<IfModule mod_rewrite.c>
#turns it on
RewriteEngine on

#if the requested url isn't a file or a dir
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#process index.php, no matter what was in the url
RewriteRule ^ index.php [L]
</IfModule>

(note that .htaccess must also have permissions to overwrite the apache's current settings)

in the index.php you can check what url was sequested and process the info

$whatTheUserRequested=$_SERVER['REQUEST_URI'];
$parameterArray=explode('/', $whatTheUserRequested);

//check the first param.
switch($parameterArray[1])
{
//if it was something like "thesite.com/main/fsadf/hgfdsgsdf/...."
case "main":
include "mainPage.php";
//....or do something else
break;

//if it was something like "thesite.com/login/asdfe/xxxx/...."
case "login":
include "loginPage.php";
//....or do something else
break;

default:
//here you MUST check if $parameterArray[1] is a username
if(check in db if $parameterArray[1] is user)
{
include "userPage.php";
}
else
{
include "pageNotFound.html"
};
}

You may check also for the 3rd or 4th parameter in the included php's, to process url's like "mysite.com/johnny95/edit"

How to create a Profile URL for a user using $_GET['id']

As stated above, you don't need to save a profile URL to the database. I'm guessing all profile URLs are going to follow some standard form (i.e. www.example.com/profile.php?id=1)?

Well, if you saved all of those in your database and then you decided you were going to change the format to something like www.example.com/profile/1 you're going to have a lot of out-of-date data in your database. You're going to have to go through each record and update it, and that could be dangerous on a database table with say, millions of rows.

Therefore, the solution is to have a script that takes a parameter. Say profile.php. As above, you would check for the profile using the data in the $_GET array:

<?php
if (isset($_GET['id'])) {
$id = mysql_real_escape_string($_GET['id']);
$sql = "SELECT * FROM members WHERE id = '$id' LIMIT 1";
$res = mysql_query($sql);
if (mysql_num_rows() > 0) {
$member = mysql_fetch_object($res);
// handle displaying of member's profile here
}
else {
// member does not exist with ID
}
}
?>

That way, if you decide to change the script name or use search engine-friendly URLs, you don't need to change your database structure.



Related Topics



Leave a reply



Submit