这个问题似乎含糊不清,我道歉,所以我会在这里尝试解释一下。基本上,我有2个.php文件,index.php
和php.php
。在index.php
上,用户会在表单中输入用户名,然后提交输入php.php
,然后抓取用户名(使用$_POST[]
方法。)并在该页面上显示数据。此时的URL看起来像这样;
http://minecraftnamespy.esy.es/php.php
。
我该怎么做才能将用户输入的用户名添加到网址上? E.g;
用户输入:_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> •
<span>©2015</span> •
<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>
答案 0 :(得分:2)
您需要一个GET请求,该请求在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> •
<span>©2015</span> •
<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>