I am trying to setup a website that converts a Steam user ID into an auth ID. It will ask for the visitor to input their regular Steam ID and then hit a button to convert it to auth ID. Steam provides us with the function for ID conversion from one type to the other.
Steam function for converting IDs:
function convert_steamid_to_accountid($steamid)
{
$toks = explode(":", $steamid);
$odd = (int)$toks[1];
$halfAID = (int)$toks[2];
$authid = ($halfAID*2) + $odd;
echo $authid;
}
Below is my attempt at setting up a basic HTML page that gets user input and then uses the function to convert that input to something else.
<INPUT TYPE = "Text" VALUE ="ENTER STEAM:ID" NAME = "idform">
<?PHP
$_POST['idform'];
$steamid = $_POST['idform'];
?>
Also, this is what the default Steam user ID looks like:
STEAM_0:1:36716545
Thank you for all the help!
If you can make it into two seperate files, then do so.
foo.html
<form method="POST" action="foo.php">
<input type="text" value="ENTER STEAM:ID" name="idform" />
<input type="submit" />
</form>
foo.php
<?php
function convert_steamid_to_accountid($steamid)
{
$toks = explode(":", $steamid);
$odd = (int)$toks[1];
$halfAID = (int)$toks[2];
$authid = ($halfAID*2) + $odd;
echo $authid;
}
$id = $_POST['idform'];
convert_steamid_to_accountid($id)
?>
if you don't have an option of making two seperate files, you can add the php code to 'foo.html' file and make the form to submit to the same file. However if you do this, check if the file is getting requested the first time, or it is requested because the form is submitted, BEFORE you call convert_steamid_to_accountid() function.
You can do this by:
if ($_SERVER['REQUEST_METHOD']=='POST'){
// your php code here that should be executed when the form is submitted.
}
Related
I have four input texts and one submit button
enter image description here
what I want is for the results of the input text to turn into a link that I use as a whatsapp message like this.
https://wa.me/628123456789?text=Hai%20My%20Name%20Andi
what should i do?
this is my code
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<!-- https://wa.me/628123456789?text=textInput1%20textInput2%20textInput3%20textInput4 -->
<form action="https://wa.me/628123456789?text=textInput1%20textInput2%20textInput3%20textInput4" method="post">
<input type="text" name="textInput1" id="textInput1" ><br><br>
<input type="text" name="textInput2" id="textInput2" ><br><br>
<input type="text" name="textInput3" id="textInput3" ><br><br>
<input type="text" name="textInput4" id="textInput4" ><br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
//target url --> https://wa.me/628123456789?text=textInput1%20textInput2%20textInput3%20textInput4
$url = null;
if(isset($_POST['submit']))
{
$textInput1 = $_POST['textInput1'];
$textInput2 = $_POST['textInput2'];
$textInput3 = $_POST['textInput3'];
$textInput4 = $_POST['textInput4'];
$url = "https://wa.me/628123456789?text=".$textInput1." ".$textInput2." ".$textInput3." ".$textInput4;
}
?>
</body>
</html>
this is var_dump result for my code
enter image description here
Simply set your form to method post, see my code below. Add name attributes to your inputs fields and submit button so you can retrieve their values through http post. Then check to see if the submit button has been posted using isset($_POST['submit']). If isset, we assign the values of your input fields to variables in order to recreate the urls post key/value pairs.
IMPORTANT NOTE: I am not covering cleaning of your input fields make sure to read up on proper cleaning of inputs depending on what you are allowing to be processed by the back-end code in order to recreate your url.
<form action="/https://wa.me/628123456789?text=textInput1%20textInput2%20textInput3%20textInput4" method="post">
<input type="text" name="textInput1" id="textInput1" ><br><br>
<input type="text" name="textInput2" id="textInput2" ><br><br>
<input type="text" name="textInput3" id="textInput3" ><br><br>
<input type="text" name="textInput4" id="textInput4" ><br><br>
<input type="submit" name="submit" value="Submit">
</form>
PHP:
//target url --> https://wa.me/628123456789?text=textInput1%20textInput2%20textInput3%20textInput4
$url = null;
if(isset($_POST['submit'])){
$textInput1 = $_POST['textInput1'];
$textInput2 = $_POST['textInput2'];
$textInput3 = $_POST['textInput3'];
$textInput4 = $_POST['textInput4'];
$url = "https://wa.me/628123456789?text=".$textInput1." ".$textInput2." ".$textInput3." ".$textInput4;
}
Example OUTPUT. Simply outputting the url within an html page in example:
In your html simply echo out the variable $url. it will only display in your code when it is actually set. <?=$url?> or <?php echo $url;?>
NOTE: If you are trying to place the link into your action attribute of your form prior to submitting the page so that the values set in the input fields are a part of the forms action, you will need to get the values before submitting the page by using JS or JQuery and getting the values of the inputs on change or something of that nature, then build the url in js/jquery then set your forms attribute action using JS/Jquery.
EDIT: I want the result of the input text to be a whatsapp message, so it will be placed in the url link. I have updated my post
Okay, to redirect user once the form is filled out and you have sanitized your inputs, set the url in your a header() function and redirect your user to the desired url.
*Make sure you remove the action attribute from your form as you will be redirecting using the php header() function instead.
if(isset($_POST['submit'])){
// I am using filter_var(FILTER_SANITIZE_STRING) in this example.
$textInput1 = filter_var ( $_POST['textInput1'], FILTER_SANITIZE_STRING);
$textInput2 = filter_var ( $_POST['textInput2'], FILTER_SANITIZE_STRING);
$textInput3 = filter_var ( $_POST['textInput3'], FILTER_SANITIZE_STRING);
$textInput4 = filter_var ( $_POST['textInput4'], FILTER_SANITIZE_STRING);
// make sure to test this url by echoing it out before you run the header redirect.
$url = urlencode("https://wa.me/628123456789?text=".$textInput1." ".$textInput2." ".$textInput3." ".$textInput4);
header("Location: $url");
exit();
}
Using a conditional with a foreach loop with header to contruct url from post values:
if(isset($_POST['submit'])){
$url = "https://wa.me/628123456789?text="; // declare the core of your url without the post values
$i = 1; // increment
$k = 0; // key value for $inputs
$userinput = ''; // empty variable to hold user inputs for encoding
$num = count($_POST) - 1; // count the number of items in the array to properly format spaces in url string subtract one for submit button
foreach($_POST as $value){ // run a foreach loop on the $_POST
if($value !== "Submit"){ // we remove the submit post value from our array by omitting it using does not equal
$inputs[] = filter_var ( $value, FILTER_SANITIZE_STRING); // create a new array and push values into it
if($i < $num){ // all but last iterations will produce the space
$url .= $input[$k]." ";
}else{ // last iteration will not have a space
$url .= $input[$k];
}
}
}
$url .= urlencode($userinput);
echo $url; // for testing purposes to make sure the string is populating the input values as you have entered them delete this line after testing.
//header("Location: $url"); <-- Uncomment this line to redirect
//exit(); <-- uncomment exit() if you uncomment header() to close after redirect to make sure code stops on this page.
}
Just add the function urlencode() before the content:
$url = urlencode("https://wa.me/628123456789?text=".$textInput1." ".$textInput2." ".$textInput3." ".$textInput4);
This is the seat booking form for a cinema's website that I need to check the number of seats available when users select their seats.
There are 3 seat types (S, F, B) with the capacity of 40, 12, 13 respectively.
In HTML I have:
<input name="Stype" type="number" value="" max="<?php echo /the number of available seats/?>" min="0" />
<input name="Ftype" type="number" value="" max="<?php echo /the number of available seats/?>" min="0" />
<input name="Btype" type="number" value="" max="<?php echo /the number of available seats/?>" min="0" />
<input type="submit" name="submit" value="submit" />
Then I use php to get user inputs like this:
<?php
session_start();
$SSeats = '';
$FSeats = '';
$BSeats = '';
if(isset($POST['submit']))
{
//get inputs by method POST
//pass input data to the next shopping-cart page via SESSION
$_SESSION['SSeats'] = $SSeats;
$_SESSION['FSeats'] = $FSeats;
$_SESSION['BSeats'] = $BSeats;
}
else
{
$_SESSION['SAvai'] = 40 - $_SESSION['SSeats'];
$_SESSION['FAvai'] = 12 - $_SESSION['FSeats'];
$_SESSION['BAvai'] = 13 - $_SESSION['BSeats'];
//Then I write above SESSIONs into a file, then output the number of
//seats available to the HTML code
}
?>
Before I test this code, I did tested many things else and the SESSIONs already have values in it, so the code can run well. But when I reopen the website and go to this page, it throws an exception that the indexes 'SSeats', 'FSeats', 'BSeats' are undefined.
The requirement also includes after users view their booking cart, they can add/remove seats in the reservation.
So could somebody suggest me the way to fix it or how to make it easier.
You have two errors:
Change $POST to $_POST
if(isset($_POST['submit']))
And you need $_POST['html_element_name'] to get the value
$_SESSION['SSeats'] = $_POST['SSeats'];
$_SESSION['FSeats'] = $_POST['FSeats'];
$_SESSION['BSeats'] = $_POST['BSeats'];
The code:
if(isset($_POST['submit']))
{
//get inputs by method POST
//pass input data to the next shopping-cart page via SESSION
$_SESSION['SSeats'] = $_POST['SSeats'];
$_SESSION['FSeats'] = $_POST['FSeats'];
$_SESSION['BSeats'] = $_POST['BSeats'];
}
else
{
$_SESSION['SAvai'] = 40 - $_SESSION['SSeats'];
$_SESSION['FAvai'] = 12 - $_SESSION['FSeats'];
$_SESSION['BAvai'] = 13 - $_SESSION['BSeats'];
//Then I write above SESSIONs into a file, then output the number of
//seats available to the HTML code
}
?>
You have a typo in the if(isset). $_POST is missing an underscore, as well as you were not calling the $_POST variables when defining the session variables. You were calling blank variables.
<?php
session_start();
$SSeats = '';
$FSeats = '';
$BSeats = '';
if(isset($_POST['submit'])) //typo fixed here
{
//get inputs by method POST
//pass input data to the next shopping-cart page via SESSION
$_SESSION['SSeats'] = $_POST['SSeats'];
$_SESSION['FSeats'] = $_POST['FSeats'];
$_SESSION['BSeats'] = $_POST['BSeats'];
}
else
{
$_SESSION['SAvai'] = 40 - $_SESSION['SSeats'];
$_SESSION['FAvai'] = 12 - $_SESSION['FSeats'];
$_SESSION['BAvai'] = 13 - $_SESSION['BSeats'];
//Then I write above SESSIONs into a file, then output the number of
//seats available to the HTML code
}
?>
here is what I want to do, i want to build a program that prompts the user for a string, stores the string in an array and prompts the user for another string, stores that string in an array and repeats the process 5 times, but every time I run the program, the string gets erased and it stores the same string 5 times, how can i modify this php code such that it does what I said? I tried using readline() but it doesn't work, it says it is an undefined function.
<form action = "index.php" method = "GET">
Name: <input type = "text" name = "tell" >
<br><br>
<input type = "submit" value = "submit">
</form>
<?php
$tester = array();
$counter = 5;
while ($counter > 0)
{
echo "please enter a criteria";
$name = $_GET['tell'];
array_push($tester,$name);
$counter = $counter - 1;
}
print_r($tester);
?>
PHP only runs when the page loads, so your code is only getting one value from $_GET. In order to do this, you have to start a session and store your array in a session variable.
<?PHP
session_start();
if($_GET['tell']) {
$tester = $_SESSION['tester'];
array_push($tester,$_GET['tell']);
$_SESSION['tester'] = $tester;
}
if(count($tester) < 5) {
//display your form to ask the user for more information
}
I'm trying to make a simple captcha in PHP, but it does not work. The query is not currently executing. This is my current code:
<?php
$Random = rand(1, 100);
$Random2 = rand(1,100);
echo "Result: ".$Random." + ".$Random2." ?";
?>
<input type="text" name="r_input"/><br />
$Cap = mysql_real_escape_string($_POST['r_input']);
$Result = $Random+$Random2;
if(isset($_POST['myButton']) and trim($Var) and trim($Var2) and trim($Var3) and $Cap==$Result){
//My Query
}
When you use rand() to generate 2 values, and show those 2 values, and give the form for the user to enter the answer, ...
... the user enters the answer and submits back to the server ...
... the server gets the answer, and then GENERATES 2 NEW VALUES, that don't correspond to the answer given by the user.
Try using session variables to store the generated values in, and match against when the user submits the form!
<?php
session_start();
$captcha_id = 'captcha_' . rand();
$_SESSION['$captcha_id']['val1'] = rand(1,1000);
$_SESSION['$captcha_id']['val2'] = rand(1,1000);
echo "
<form action='' method='post'>
<p>Result: {$_SESSION['$captcha_id']['val1']} + {$_SESSION['$captcha_id']['val2']} = </p>
<input type='hidden' name='captcha_id' value='{$captcha_id}' />
<input type='text' name='captcha_answer' />
<p>?</p>
</form>
";
if (
isset($_POST['captcha_id'])
&& isset($_SESSION[$_POST['captcha_id']])
&& isset($_POST['captcha_answer'])
&& $_SESSION[$_POST['captcha_id']]['val1'] + $_SESSION[$_POST['captcha_id']]['val2'] == intval($_POST['captcha_answer'])
) {
unset($_SESSION[$_POST['captcha_id']]); // don't let this answer be reused anymore.
// do allowed stuff
}
?>
Because $Random and $Random2 have a different value each time.
When you show the form for the first time, they may have the values $Random = 12 and $Random2 = 26. The User sees those, adds them up correctly and types in 38 (which is the correct answer for those two values). The answer is sent to the script again, the values of $Random and $Random2 are generated again (this time as $Random = 23 and $Random2 = 30 which equals 53) and the answer the user has sent is not correct any more.
So you would need to store those values in hidden fields and add these up, instead of the generated ones, like so:
<input type="hidden" name="rand_1" value="<?php echo $Random; ?>">
<input type="hidden" name="rand_2" value="<?php echo $Random2; ?>">
<?php
if ($_POST['rand_1'] + $_POST['rand_2'] == $_POST['r_input']) {
// Query etc.
EDIT: As suggested by #nl-x you should use the Session variables instead of hidden fields to prevent abuse of the captcha:
<?php
$Random = $_SESSION['rand_1'] = rand(1, 100);
$Random2 = $_SESSION['rand_2'] = rand(1,100);
echo "Result: ".$Random." + ".$Random2." ?";
?>
And check those values against the given result afterwards:
<?php
$Cap = mysql_real_escape_string($_POST['r_input']);
$Result = $_SESSION['rand_1'] + $_SESSION['rand_2'];
if ($Result == $Cap) {
// ...
You never re-enter PHP mode after you output your form field:
<input type="text" name="r_input"/><br />
<?php // <----this is missing
$Cap = mysql_real_escape_string($_POST['r_input']);
Pardon me, but you are not making a real captcha. The purpose of the captcha is to distinguish the human from the bots. I would highly suggest you to pick a image database, and randomize a function to call a image. Internally, i would check if the text/description of the image matches with what the user typed.
The only thing you will rand() is what image to load from your image database.
That's a not-healthy way to do it, and there are plenty of better ways to do this. But it's more closer to a captcha than just your current code.
There is also a lot of libraries and engines that can do the job for you.
I'm not a pro at PHP, or even programming at all, but i think you're going to the wrong side - your code won't block any... malicious actions at all, or whatever kind of action that you will try to prevent with the captcha.
Search google for the libraries. PhpCaptcha is one of them. And here is a very simple quickstart guide for phpcaptcha.
Here's a code example, extracted from PHPCaptch that I linked above.
At the desired position in your form, add the following code to display the CAPTCHA image:
<img id="captcha" src="/securimage/securimage_show.php" alt="CAPTCHA Image" />
Next, add the following HTML code to create a text input box:
<input type="text" name="captcha_code" size="10" maxlength="6" />
[ Different Image ]
On the very first line of the form processor, add the following code:
<?php session_start(); ?>
The following php code should be integrated into the script that processes your form and should be placed where error checking is done. It is recommended to place it after any error checking and only attempt to validate the captha code if no other form errors occured. It should also be within tags.
include_once $_SERVER['DOCUMENT_ROOT'] . '/securimage/securimage.php';
$securimage = new Securimage();
This includes the file that contains the Securimage source code and creates a new Securimage object that is responsible for creating, managing and validating captcha codes.
Next we will check to see if the code typed by the user was entered correctly.
if ($securimage->check($_POST['captcha_code']) == false) {
// the code was incorrect
// you should handle the error so that the form processor doesn't continue
// or you can use the following code if there is no validation or you do not know how
echo "The security code entered was incorrect.<br /><br />";
echo "Please go <a href='javascript:history.go(-1)'>back</a> and try again.";
exit;
}
Following the directions above should get Securimage working with minimal effort.
This code is included here as well.
Good luck!
I want to be able to add new values to the array called playlist using functions (is mandatory). But everytime I input another value it replaces the old one instead of just adding on the end of the array. What is wrong?
<?php
$playlist = array("Be more.mp3", "Drift Away.mp3", "Panda Sneeze.mp3");
function add_songs_playlist() {
global $playlist;
?>
<form method = "post">
<input type = "text" name = "name1"/>
<input type = "submit" name = "submit1"/>
</form>
<?php
if (isset($_POST['submit1'])) {
$newsong = $_POST['name1'];
array_push($playlist, $newsong);
}
}
add_songs_playlist();
foreach ($playlist as $value) {
echo $value."<br>";
}
PHP values aren't saved between requests. Every time this page loads, $playlist starts out with those same three values you assign.
If you want to keep data around longer, you'll need to save it somewhere—a cookie, a file, the session, a database, etc.
Alternatively, you could store the values in the HTML itself, by printing an <input type="hidden"> for each song in the playlist. Then when the user enters a new song, you have the new song plus the full list of all the old ones.
Just write the playlist to a file
<?php
if (isset($_POST['submit1'])) {
$song = "\n".$_POST['name1'];
$file = fopen("songs.txt", 'a');
fwrite($file, $song);
fclose($file);
}
$file = file("songs.txt");
foreach ($file as $song) {
echo $song . "</br>";
}
?>
<form method = "post">
<input type = "text" name = "name1"/>
<input type = "submit" name = "submit1"/>
</form>
There is a general mistake you make:
you have to understand that each time you transmit a value the way you do, you php script is started again. It restarts without anything left from the last time.
To be able to take over values the way you want to you have to store those values in a persistant manner after having pushed them to the array. During the next run you first have to read the existing values from that persistant storage, fill the array and only then you can add another value on top of that array. This is a general principle, the basic way this kind of framework works.
Typically a database is used as persistant storage, but also files might be interresting, depending on how many request and how much data you plan to process.
Here is an small mod of your code. I removed the function and stored your array within the form itself.
This one will work without storing any information on the server, bet I don't think it wil be very usable. You should store your content like suggested by #Anonymous in previous reply.
Working code:
<?php
// First check if I get a POST request with list of songs
if (isset($_POST['playlist'])) {
$playlist = $_POST['playlist'];
} else { // Else set my default list
$playlist = array("Be more.mp3", "Drift Away.mp3", "Panda Sneeze.mp3");
}
// You need to get the added son BEFORE you generate the FORM,
// so it can be a part of the form
if (isset($_POST['name1'])) {
$playlist[] = $_POST['name1'];
}
?>
<form method = "post">
<?php
// This loop stores your songs as hidden values within the form,
// so you will get them with your next post
foreach($playlist as $song) {
?>
<input type = "hidden" name = "playlist[]" value = "<?php echo $song?>">
<?php
}
?>
<input type = "text" name = "name1"/>
<input type = "submit" name = "submit1"/>
</form>
<?php
foreach ($playlist as $value) {
echo $value."<br>";
}
An example specific to your repost code would be:
if (isset($_POST['submit1'])) {
$newsong = $_POST['name1'];
$playlist[] = $newsong;
}
Also an error may be the fact your form is within a function... why not just do this
<?php
$playlist = array("Be more.mp3", "Drift Away.mp3", "Panda Sneeze.mp3");
?>
<form method = "post">
<input type = "text" name = "name1"/>
<input type = "submit" name = "submit1"/>
</form>
<?php
if (isset($_POST['submit1'])) {
$newsong = $_POST['name1'];
$playlist[] = $newsong;
}
foreach ($playlist as $value) {
echo $value."<br>";
}
?>
I am not sure if you care if the page refreshes. But if you are fine with a page refresh this should work. If you do not want a refresh you can either save your variables in javascript and do your work in that and use the form button onClick as a function call. Or if you want to work your variables in PHP in-order to reset your php variable without refreshing you will need to make an AJAX call to call back JSON to update your PHP variable.