how do i insert a variable? from a form into a fil

2019-09-16 06:14发布

问题:

how do i insert a variable from a form into a file_get_contents("link+formdata")

the variable being submitted via a form and contains full stops,Numbers,letter

Example: danny.29 would replace HERE in the link below:

file_get_contents("http://userapi.website.com/HERE");

As you can see i'm very new to this, any help would be very much appreciated :)

index.html

<html>
<body>

<form action="add.php" method="post">
ID: <input type="text" name="add">
<input type="submit">
</form>

</body>
</html>

add.php

<html>
<body>

<?php $x=$_POST['add'];?>

<?php
echo file_get_contents("http://userapi.website.com/"echo $x;");
?>

<?php echo $_POST["file_get_contents"]; ?>

</body>
</html>

回答1:

<?php echo file_get_contents("http://userapi.website.com/" . $x); ?>

Dot is for concatenation



回答2:

<?php echo $_POST["file_get_contents"]; ?>

doesn't make sense. The POST array will only contain the add key on the first load.

echo file_get_contents("http://userapi.website.com/". $x);

will get correctly the content of the address page but to memorize it into a variable you just have to do:

$var = file_get_contents("http://userapi.website.com/". $x);

Also this script is no totally secure. You should always validate user input (your POST variable in this case). I'd suggest you to use a REGEX or a list of acceptable values for that variable.