如何根据用户名 PHP 创建 URL

How to have a URL based off a username PHP

这个问题可能看起来很模糊,我深表歉意,所以我会尽量在这里更好地解释它。基本上,我有 2 个 .php 文件,index.phpphp.php。在 index.php 上,用户将他们的用户名输入到表单中,然后他们将在其中提交输入,php.php 然后获取用户名(使用 $_POST[] 方法。)并在该页面上显示数据.此时的 URL 看起来像这样;

http://minecraftnamespy.esy.es/php.php.

我该怎么做才能将用户输入的用户名添加到 URL?例如;

用户输入:_scrunch 作为用户名。

URL now changes from http://minecraftnamespy.esy.es/php.php to http://minecraftnamespy.esy.es/php?=_scrunch

我想指出,我在这里使用 Mojang API。 我的 php.php 代码是:

<?php
  //error_reporting(E_ALL & ~E_NOTICE);
  // Load the username from somewhere
  if (
  $username = $_POST["username"]
  ) {
    //do nothing 
  } else {
    $username = "notch";
  }


  //allow the user to change the skin
  $skinChange = "<a href='https://minecraft.net/profile/skin/remote?url=http://skins.minecraft.net/MinecraftSkins/$username.png' target='_blank' </a>";

  //grabbing the users information
  if ($content = file_get_contents('https://api.mojang.com/users/profiles/minecraft/' . urlencode($username))
  ) {
    $userSkin = "<img src='https://mcapi.ca/skin/3d/$username' />";
  } else {
    $content = file_get_contents('https://api.mojang.com/users/profiles/minecraft/' . urlencode($username) . '?at=0');
  if( $http_response_header['0'] == "HTTP/1.1 204 No Content") {
      echo "Not a valid Minecraft Username! <a href='index.php'><button>Search Again?</button></a>";
      die;
  }
  $json = json_decode($content);

     foreach ($json as $currentName) {
     $currentName = $currentName;
  }

     $userSkin = "<img src='https://mcapi.ca/skin/3d/$currentName' />";
  }



  // Decode it
  $json = json_decode($content);

  // Check for error
  if (!empty($json->error)) {
      die('An error happened: ' . $json->errorMessage);
  }

  // Save the uuid
  $uuid = $json->id;

  // Get the history (using $json->uuid)
  $content = file_get_contents('https://api.mojang.com/user/profiles/' . urlencode($uuid) . '/names');

  // Decode it
  $json = json_decode($content);

  $names = array(); // Create a new array

  foreach ($json as $name) {
      $input = $name->name;

      if (!empty($name->changedToAt)) {
          // Convert to YYYY-MM-DD HH:MM:SS format
          $time = date('Y-m-d H:i:s', $name->changedToAt);

          $input .= ' (changed at ' . $time . ')';
      }

      $names[] = $input; // Add each "name" value to our array "names"

  }

  //url to users 2D head (avatar)
  $usersAvatar = "https://mcapi.ca/avatar/2d/$input/55";

  //user's Avatar as favivon
  $usersFavicon = "<link rel='shortcut icon' href='$usersAvatar' type='image/png' />";
  //use $uuid tp grab UUID of the user - ---- - - - use $names to get name history of the user.



  ?>

  <html>
  <head>
    <?php echo $usersFavicon;?>
    <title><?php echo $username?>'s Information</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
    <title>Find a player skin!</title>
  <style>
  body {
    background-image: url(http://minecraftnamespy.esy.es/grad.jpg);
    background-position: bottom;
    background-repeat: no-repeat;
    background-size: 100% 500px;
  }
  #content {
    margin-left: auto;
    margin-right: auto;
    width: 60%;
  }
  img.logo {
    margin-right: auto;
    margin-left: auto;
    display: block;
    padding: 30px;
    max-width: 100%;
    height: auto;
    width: auto;
  }
  .center {
    margin-left: auto;
    margin-right: auto;
  }
  .footer {
          text-align: center;
  }
  p.responsive {
    word-wrap: break-word;
  }

  </style>
  </head>
  <body>
  <a href="https://github.com/WelshJoeyy/MinecraftNameSpy"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://minecraftnamespy.esy.es/source.png" alt="View Source on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png"></a>

  <!--debug -->

  <?php ?>
  <div id="content">

  <div class="col-md-12">
    <img class="logo" src="http://minecraftnamespy.esy.es/logo.png">
  </div>

  <div class="col-md-12">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h3 class="panel-title"><?php echo $username;?>'s UUID</h3>
    </div>
    <div class="panel-body">
      <p class="responsive"><?php echo $uuid;?></p>
    </div>
  </div>
  </div>

  <div class="col-md-8">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h3 class="panel-title"><?php echo $username;?>'s Name History (Oldest to Most Recent)</h3>
    </div>
    <div class="panel-body">
     <?php echo implode(', <br>', $names) ;?>
    </div>
  </div>
  </div>

  <div class="col-md-4">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h3 class="panel-title"><?php echo $username;?>'s Avatar</h3>
    </div>
    <div class="panel-body">
        <div class="center">
        <?php echo $userSkin;?>
     </div>
     <p><?php echo $skinChange;?>Change this skin to yours!</a></p>
    </div>
  </div>
  </div>
  <div class="col-md-12">
  <div class="btn-group pull-right" role="group" aria-label="">
    <a href="index.php"><button type="button" class="btn btn-default">Search another Username?</button></a>
  </div>
  </div>
  <div class="footer">
    <span>Created by _scrunch</span> &#8226;
    <span>&copy;2015</span> &#8226;
    <span>Find me on <a href="http://www.planetminecraft.com/member/tacolover22/" target="_blank">PMC</a></span>

    <form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
  <input type="hidden" name="cmd" value="_s-xclick">
  <input type="hidden" name="hosted_button_id" value="Y8MWQB9FCUTFJ">
  <input type="image" src="https://www.paypalobjects.com/en_US/GB/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal – The safer, easier way to pay online.">
  <img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1">
  </form>

  </div>
  </div>
  </body>
  </html>   

这是用于在 index.php 上提交用户名的表单。

<form action="php.php" method="GET">
          <div class="form-group">
            <label for="username">Username:</label>
            <input type="text" class="form-control" id="username" name="username" placeholder="Enter username">
          </div>
          <button type="submit" class="btn btn-default">Submit</button>
</form>

你想要一个GET请求,在URL中以http://example.com/index.php?username=_scrunch

的形式可见

POST 请求是不可见的(即它不会出现在 URL 中)并且通常用于发送敏感数据(尽管 POST 不足以保证数据安全!)。

将您的 POST 转换为 GET 是从

更改的简单问题

$username = $_POST["username"]

$username = $_GET["username"]

你可以使用这个,注意额外包含

if (!$_GET["username"]) {
    exit;
}

如果 username 不存在,这会导致您的页面退出加载并防止页面被滥用。

<?php
  //error_reporting(E_ALL & ~E_NOTICE);
  // Load the username from somewhere
  if (!$_GET["username"]) {
    exit;
  }
  if (
  $username = $_GET["username"]
  ) {
    //do nothing 
  } else {
    $username = "notch";
  }


  //allow the user to change the skin
  $skinChange = "<a href='https://minecraft.net/profile/skin/remote?url=http://skins.minecraft.net/MinecraftSkins/$username.png' target='_blank' </a>";

  //grabbing the users information
  if ($content = file_get_contents('https://api.mojang.com/users/profiles/minecraft/' . urlencode($username))
  ) {
    $userSkin = "<img src='https://mcapi.ca/skin/3d/$username' />";
  } else {
    $content = file_get_contents('https://api.mojang.com/users/profiles/minecraft/' . urlencode($username) . '?at=0');
  if( $http_response_header['0'] == "HTTP/1.1 204 No Content") {
      echo "Not a valid Minecraft Username! <a href='index.php'><button>Search Again?</button></a>";
      die;
  }
  $json = json_decode($content);

     foreach ($json as $currentName) {
     $currentName = $currentName;
  }

     $userSkin = "<img src='https://mcapi.ca/skin/3d/$currentName' />";
  }



  // Decode it
  $json = json_decode($content);

  // Check for error
  if (!empty($json->error)) {
      die('An error happened: ' . $json->errorMessage);
  }

  // Save the uuid
  $uuid = $json->id;

  // Get the history (using $json->uuid)
  $content = file_get_contents('https://api.mojang.com/user/profiles/' . urlencode($uuid) . '/names');

  // Decode it
  $json = json_decode($content);

  $names = array(); // Create a new array

  foreach ($json as $name) {
      $input = $name->name;

      if (!empty($name->changedToAt)) {
          // Convert to YYYY-MM-DD HH:MM:SS format
          $time = date('Y-m-d H:i:s', $name->changedToAt);

          $input .= ' (changed at ' . $time . ')';
      }

      $names[] = $input; // Add each "name" value to our array "names"

  }

  //url to users 2D head (avatar)
  $usersAvatar = "https://mcapi.ca/avatar/2d/$input/55";

  //user's Avatar as favivon
  $usersFavicon = "<link rel='shortcut icon' href='$usersAvatar' type='image/png' />";
  //use $uuid tp grab UUID of the user - ---- - - - use $names to get name history of the user.



  ?>

  <html>
  <head>
    <?php echo $usersFavicon;?>
    <title><?php echo $username?>'s Information</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
    <title>Find a player skin!</title>
  <style>
  body {
    background-image: url(http://minecraftnamespy.esy.es/grad.jpg);
    background-position: bottom;
    background-repeat: no-repeat;
    background-size: 100% 500px;
  }
  #content {
    margin-left: auto;
    margin-right: auto;
    width: 60%;
  }
  img.logo {
    margin-right: auto;
    margin-left: auto;
    display: block;
    padding: 30px;
    max-width: 100%;
    height: auto;
    width: auto;
  }
  .center {
    margin-left: auto;
    margin-right: auto;
  }
  .footer {
          text-align: center;
  }
  p.responsive {
    word-wrap: break-word;
  }

  </style>
  </head>
  <body>
  <a href="https://github.com/WelshJoeyy/MinecraftNameSpy"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://minecraftnamespy.esy.es/source.png" alt="View Source on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png"></a>

  <!--debug -->

  <?php ?>
  <div id="content">

  <div class="col-md-12">
    <img class="logo" src="http://minecraftnamespy.esy.es/logo.png">
  </div>

  <div class="col-md-12">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h3 class="panel-title"><?php echo $username;?>'s UUID</h3>
    </div>
    <div class="panel-body">
      <p class="responsive"><?php echo $uuid;?></p>
    </div>
  </div>
  </div>

  <div class="col-md-8">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h3 class="panel-title"><?php echo $username;?>'s Name History (Oldest to Most Recent)</h3>
    </div>
    <div class="panel-body">
     <?php echo implode(', <br>', $names) ;?>
    </div>
  </div>
  </div>

  <div class="col-md-4">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h3 class="panel-title"><?php echo $username;?>'s Avatar</h3>
    </div>
    <div class="panel-body">
        <div class="center">
        <?php echo $userSkin;?>
     </div>
     <p><?php echo $skinChange;?>Change this skin to yours!</a></p>
    </div>
  </div>
  </div>
  <div class="col-md-12">
  <div class="btn-group pull-right" role="group" aria-label="">
    <a href="index.php"><button type="button" class="btn btn-default">Search another Username?</button></a>
  </div>
  </div>
  <div class="footer">
    <span>Created by _scrunch</span> &#8226;
    <span>&copy;2015</span> &#8226;
    <span>Find me on <a href="http://www.planetminecraft.com/member/tacolover22/" target="_blank">PMC</a></span>

    <form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
  <input type="hidden" name="cmd" value="_s-xclick">
  <input type="hidden" name="hosted_button_id" value="Y8MWQB9FCUTFJ">
  <input type="image" src="https://www.paypalobjects.com/en_US/GB/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal – The safer, easier way to pay online.">
  <img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1">
  </form>

  </div>
  </div>
  </body>
  </html>