【问题标题】:How to have a URL based off a username PHP如何根据用户名 PHP 获取 URL
【发布时间】:2015-03-13 22:00:51
【问题描述】:

这个问题可能看起来很模糊,我深表歉意,所以我会在这里尝试更好地解释它。基本上,我有 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

我想指出,我在这里与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\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 请求,而不是一个 POST 请求。
  • @ʎǝʞuoɯɹǝqʎɔ 我可以简单地将$_POST["username"] 更改为$_GET["username"] 吗?
  • 是的,但请记住我在下面的回答中概述的内容(在我提交后稍后会出现)。

标签: php url post get


【解决方案1】:

你想要一个 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\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 表格有问题。我现在将发布我在原始问题中使用的表格。
  • @JLWillliamsUK 您确实在 URL 中使用了 username 变量,是吗?例如,您应该使用http://minecraftnamespy.esy.es/php?username=_scrunch
  • 不!仍然收到错误 404。我是否遗漏了一些明显的东西?当然只是将POST 方法更改为GET 不会生成带有用户名的URL?表单方法应该是 POST 还是 GET ?目前是GET
  • @JLWillliamsUK 你从来没有给出正确的链接,应该是这样的:minecraftnamespy.esy.es/php.php?username="_scrunch"。
  • 是的!工作正常!再次感谢!如此简单的修复!
猜你喜欢
  • 1970-01-01
  • 2015-07-17
  • 1970-01-01
  • 2016-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-23
  • 1970-01-01
相关资源
最近更新 更多