繁体   English   中英

如何在/之后的url中获取用户名?

[英]How to get a username in the url after the /?

我正在使用 PHP 创建一个社交媒体平台。 我为您正在查看的用户提供了一个完全可观的页面,该页面正在根据您尝试在 URL 中查看的人进行调整。 如果您转到 profile.php?user=(USERNAME) 它将显示该用户的数据。

但我想要一些额外的东西。 我不希望我的用户必须填写这样一个困难的 URL。 我希望他们能够输入 profile/(USERNAME) 并显示他们正在查看的用户的数据。

那么我怎样才能使来自 ?user=(USERNAME) 的 GET 在 /(USERNAME) 中成为可能。

我希望我的问题很清楚。 期待问题或答案。

亲切的问候,塞尔

以下应该适合您。

设置输入字段并将值设置为/profile -> <input value="profile/">(例如请参见下面的 HTML 代码) ,以便自动填充该字段以供用户输入。 然后通过 php 运行您的 post 值并检查是否设置,清理(有关更多信息请参见https://www.php.net/manual/en/function.urlencode.php ,然后构建您的 url,最后通过标题运行它重定向以重定向到个人资料页面。

下面的例子:

HTML:

<form method="post"><!--// No action needed as we will redirect with php `header()` if submit is pushed. //-->
  View Users Profile:
  <input type="text" value="profile/" name="searchUser" id="searchUser" ><br><br>

  <input type="submit" name="submit" value="Submit">

  <?=$url?><!--// Used simply to echo out your url string for testing purposes, if you are testing, simply comment out the header line in the php so the code does not redirect you to the url //-->
</form>

PHP:

$url = null; // Declare an empty variable to display URL for testing purposes 
// * Not needed if your not displaying this url to test the output of your url string

// Make sure the $_POST globals are set using isset()
if(isset($_POST['submit']) && isset($_POST['searchUser'])){ 

    // Remove the `profile/` string from the input value so we only have the user being searched
    $post = str_replace("profile/","",$_POST['searchUser']);

    // More sanitation may be needed depending on what you allow your users to use for usernames
    $cleanPost = filter_var ( $post, FILTER_SANITIZE_STRING);

    // Get the server host
    $host = $_SERVER['HTTP_HOST'];

    // Get the directory  
    $uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');

    // Get the root
    $root = 'myawesomesocialsite.com';

    // Set the path to profile and add the field input from user
    $profile = "/profile?user=".$cleanPost;

    // Construct the URL using the declared variables
    $url = "http://$host$uri/$root$profile";

    // Redirect user using header()
    // Comment the header("Location: $url"); out to test the output of your $url string if you're getting errors with redirect
    header("Location: $url");

    exit();
}

使用函数:

function constURL($root, $dir){  
    $post = str_replace("profile/","",$_POST['textInput']);
    $cleanPost = filter_var ( $post, FILTER_SANITIZE_STRING);
    $host  = $_SERVER['HTTP_HOST'];
    $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); 
    $urlPost = "?user=".$cleanPost;
    $url = "http://$host$uri/$root$dir$urlPost";
    header("Location: $url");
    exit();
}

// USE FUNCTION
// Make sure you set the values for root and dir

if(isset($_POST['submit']) && isset($_POST['searchUser'])){
    $root = 'myawesomesocialsite.com'; 
    $dir = "/profile";
    constURL($root, $dir);
}

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM