如何拥有基于用户名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为用户名。

网址现在从http://minecraftnamespy.esy.es/php.php更改为 http://minecraftnamespy.esy.es/php?=_scrunch

我想指出的是,我在这里使用MojangAPI。我的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'9;
  }
  .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 请求,该请求以 http://example.com/index.php?username=_scrunch 的形式显示在 URL

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'9;
  }
  .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>