Jump to content

Recommended Posts

Hi

 

I have a web page where user registers. Each member is given its own URL based on the user name he gives during sign up. So for e.g. if some one entered "Smarty" then his web page URL will become

http://www.mywebsite.com/Smarty

 

On this page user's image and other details are shown. So if my website has 1000 users then 1000 URLs will be created like above.

How can I do this in PHP? Shall I use MOD_REWRITE or there is some other solution?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/62446-mod_rewrite-or-some-other-method/
Share on other sites

When a user signs up you'd add all their info into the database then using 1 php page, call this user.php, which takes a url parameter called userid the userid holds the username which is passed to user.php like this:

 

user.php?userid=Smarty

 

then in user.php you use $_GET['userid'] to get the username from the userid URL parameter. Using that userid you'd query your database to fetch what ever information you want to be shown for that user.

 

To get urls like this to work:

mysite.com/Smarty

 

You'd want to use mod_rewrite:

RewriteEngin On
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+)$ user.php?userid=$1 [NC, L]

Ok I did as you said. Opened .htaccess file and wrote that code in it (there was already some code written in that file so I appended it with new code)

Then I created a file user.php for testing in which I wrote

 

if($HTTP_GET_VARS["userid"] == "1")
    print "ONE";
else
    print "TWO";

 

Now when I run this page http://www.mywebsite.com/user.php?userid=1 then I get the following error

 

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

 

Please contact the server administrator, webmaster@mywebsite.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

 

More information about this error may be available in the server error log.

 

Not only that the other PHP pages in the same directory which were working previously are also showing the same error.

Check your servers error log. There is a problem with your .htaccess. Make sured you host allows you use mod_rewrite or they have the mod_rewrite Apache module loaded

 

Also you should use $_GET rather than $HTTP_GET_VARS. $HTTP_*_VARS has been replaced by $_*

 

Also when you go run user.php dont actually go to user.php you just do this:

mysite.com/user_name_here

 

Apache will then call user.php?userid=user_name_here without showing the file being called in address bar. This is how mod_rewrite works.

Oh... Hhold on... Sorry I had a few typos in my mod_rewrite code. Try this:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+) user.php?userid=$1 [NC,L]

I didn't test what I posted last time. The above should now work.

Cool...this one worked! thanks  ;D

But there is one problem though

 

In my user.php file I am printing $_GET["userid"] to see its value and this is happening

 

1. When I go to URL www.mywebsite.com/ali_kiyani then it prints home

2. When I go to URL www.mywebsite.com/ali_kiyani.php then it prints ali_kiyani

 

I think that 'home' in first point is the base directory which it shouldn't be printing and rather it should print 'ali_kiyani' like in point 2 but with out having to write .php in the end.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.