[英]Display user profile like site.com/user1
I want to create a simple social network with PHP, that every user has a profile that share his image and text. 我想用PHP创建一个简单的社交网络,每个用户都有一个共享其图像和文本的配置文件。
I have a problem that I want to add functionality in which users will the access to his profile like Facebook , Instagram and etc like below: 我有一个问题想要添加功能,用户可以通过该功能访问他的个人资料,例如Facebook,Instagram等,如下所示:
my website.com/user1
Not this : mywebsite.com/@user1
不是这样的: mywebsite.com/@user1
Create .htaccess
file and add this: 创建.htaccess
文件并添加以下内容:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /user.php?username=$1 [L] //update user.php?username to your users profile route.
If you have any question about .htaccess
file then please check this link . 如果您对.htaccess
文件有任何疑问,请检查此链接 。
For use with Nginx, refer to this answer https://stackoverflow.com/a/53415110/6749661 要与Nginx一起使用,请参阅此答案https://stackoverflow.com/a/53415110/6749661
There are a few ways you can do this, like @MRustamzade's answer, you can change the .htaccess file to contain code like this: 有几种方法可以执行此操作,例如@MRustamzade的答案,您可以更改.htaccess文件以包含如下代码:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /user.php?username=$1 [L] //update user.php?username to your users profile route.
You can also use a router framework, such as Altorouter which allows you to do this, and with a large number of pages. 您还可以使用带有大量页面的路由器框架,例如Altorouter 。
With Altorouter you can set a number of routing rules, a simple setup could consist of: 使用Altorouter,您可以设置许多路由规则,简单的设置可以包括:
$router = new AltoRouter();
$router->map( 'GET', '/profile/[*:username]', 'views/profile_view.php', 'home' );
$match = $router->match();
if($match) {
header("HTTP/1.1 200 OK");
require $match['target'];
} else {
header("HTTP/1.0 404 Not Found");
echo "Error 404 - File not found";
}
Using this code will allow you to go to site.com/profle/username
will go to views/profile_view.php
使用此代码将允许您转到site.com/profle/username
将转到views/profile_view.php
In order to get the username from the URL in PHP, you can use the $match variable, something like this: 为了从PHP中的URL获取用户名,可以使用$ match变量,如下所示:
$username = $match["params"]["username"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.