简体   繁体   English

显示用户个人资料,例如site.com/user1

[英]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.

相关问题 PDO SQL选择from_user =用户1 to_user =用户2反之亦然,例如from_user =用户2 to_user =用户1? - PDO SQL select from_user = user1 to_user = user2 also vice versa like from_user = user2 to_user = user1? 如何在OpenCart中嵌入像site.com/brands/BrandName/productName这样的制造商? - How to do nesting manufacturers like site.com/brands/BrandName/productName in OpenCart? 像site.com/#/xyz一样,PHP / CodeIgniter是否可以读取URL? - Any way for PHP/CodeIgniter to read the URL, when it's like site.com/#/xyz …? 有没有办法创建一个像 site.com/state/127 这样的友好 url 并且仍然指向一个特定的文件,比如 index.php - Is there a way to create a friendly url like site.com/state/127 and still point to a particular file like index.php 从PHP中传递的变量解析出“ site.com”? - Parsing out “site.com” from a passed variable in PHP? 如何使用此URL格式的$ _GET site.com/extension/rar - how to use $_GET with this url format site.com/extension/rar acf在用户个人资料上显示图像 - acf display image on user profile SELECT从user2阻止user1(反之亦然) - SELECT to Block user1 from user2 (Vice Versa) 寻找user2的朋友+与user1的共同计数 - find user2 friends + mutual count with user1 按名称而不是ID显示用户个人资料网址 - Display user profile url by name instead of ID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM