Generate Profile Page Upon Registration - PHP

扶醉桌前 提交于 2019-12-13 08:56:51

问题


Possible Duplicate:
Create vanity URLs in a LAMP configuration

Once a user registers, I want the username they have chosen to be used to generate a unique profile page viewable to anyone (not just the user). If "bob" registers, he will have a profile at website.com/bob

I have looked at similar questions that say to solve the problem with .htaccess, but they don't describe how this can be done.


回答1:


You wouldn't exactly need to generate a unique profile page upon registration. You can simply make a script like profile.php which takes a parameter user to dynamically show the profile of a given user by accessing /profile.php?user=bob. Example:

<?php
$u = $_GET['user'];
echo "<h2>Profile of ".htmlentities($u)."</h2>";
?>

.htaccess comes into play when you want to rewrite the url website.com/user/bob to point to website.com/profile.php?user=bob by using the following, for example.

RewriteEngine On
RewriteBase /

RewriteRule ^/user/([0-9a-zA-Z]*)$ /profile.php?user=$1 [NC,QSA,L]



回答2:


Jimmy Sawczuck pointed an answer, but to explain it here:

What you want to achieve in you case is to access a profile page using the username in the url. Creating php file for each user is troublesome and can lead to bugs (maintanability is awfull too)

You can create a profile.php script that use an argument called username. So your link will be

mysite.com/profile.php?username=bob

However it is not the url you want. And to save you exists the Rewrite Mod of webservers (mod_rewrite for apache, search for corresponding utility for other webservers)

This mod allow you to rewrite your url to match what you want:

Eg: website.com/bob in webbrowser is rewrited website.com/profile.php?username=bob for your server.

Which mean you users can have the pretty/tiny url, but the webserver will change it to call the wanted script. So both are happy.

To do this rewriting you use a module with rewriting rules. If you use apache, you use mod_rewrite and put the rule in your website configuration file or .htaccess in the wanted directory.

For the rule check the link given by Jimmy Sawczuck

Regards




回答3:


if you're using PHP, you can store the users information in a database and create a file in your directory using fopen('bob.php'). Then have bob.php call whatever functions you need to display user information.

You might need to randomize the user url as there are sure to be duplicates.

I don't believe .htaccess can accomplish this.



来源:https://stackoverflow.com/questions/10269628/generate-profile-page-upon-registration-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!