Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Not a member yet? Click here to register.
Forgot Password?

Help with PHP $_POST

Asked Modified Viewed 3,380 times
W
woodnotoil
W
  • Junior Member, joined since
  • Contributed 22 posts on the community forums.
  • Started 11 threads in the forums
  • Started this discussions
asked
Junior Member

I am trying to post a value from one page to another and have gotten this to actually work. However, by using select to assign the value I get a blank drop down menu next to the "Reviews" button even though I assigned the type as hidden. Is there a way to assign the value without using select so I don't have to have the drop down menu appear? Thanks for the help!

<!-- on the sending page: -->

<form action="viewpage.php?page_id=8" method="post">
<select name="id"   type="hidden">
<?php
echo "<option value='$id'></option>\n";
?>
<input type="submit" value="Reviews">
</form>


<!-- on the receiving page: -->
<?php
$id = $_POST['id'];
?>
0 replies

10 posts

A
Arda
A
Arda 10
  • Member, joined since
  • Contributed 150 posts on the community forums.
  • Started 11 threads in the forums
answered
Member

try this instead of using <select>:
echo "<imput type='hidden' name='id' value='".$id."'>";
0 replies
W
woodnotoil
W
  • Junior Member, joined since
  • Contributed 22 posts on the community forums.
  • Started 11 threads in the forums
  • Started this discussions
answered
Junior Member

Thanks! It worked perfectly once I got rid of the typo "imput" to "input".

Here is the finished code for anyone else who stumbles on this

<form action="viewpage.php?page_id=8" method="post">
<?php
echo "<input type='hidden' name='id' value='".$id."'>";
?>
<input type="submit" value="Reviews">
</form>
0 replies
G
googlebot
G
Visit the new home of the merge between Hacking Vs. Security and Security Override!
My copyright removal has been switched over from HvS to SecurityOverride.
  • Senior Member, joined since
  • Contributed 638 posts on the community forums.
  • Started 28 threads in the forums
answered
Senior Member

And while that solution is all well and good, it is not secure. I recommend just using the $id variable in the PHP code if you already have it set, instead of putting it into a hidden input field, because you still have access to the variable and you wouldn't be leaving it open to script manipulation.
0 replies
B
Basti
B
Basti 10
[PHP-Fusion Crew Member & Admin from June 2008 - December 2010]

http://basti2web.de - Support Site for my infusions
  • Veteran Member, joined since
  • Contributed 1,099 posts on the community forums.
  • Started 32 threads in the forums
answered
Veteran Member

and if you put this variable in your db, you should use the function stripinput()
<?php
$id = stripinput($_POST['id']);
?>

more about stripinput here:
http://code.starefossen.com/infusions.../index.php
0 replies
P
PolarFox
P
  • Veteran Member, joined since
  • Contributed 1,633 posts on the community forums.
  • Started 29 threads in the forums
answered
Veteran Member

... but if that $id is numeric (always), we must use isnum() ...
0 replies
M
mpkossen
M
  • Senior Member, joined since
  • Contributed 267 posts on the community forums.
  • Started 4 threads in the forums
answered
Senior Member

You should use both then :)
0 replies
S
starefossen
S
www.postexus.com - Follow Postexus on Facebook.
  • Senior Member, joined since
  • Contributed 359 posts on the community forums.
  • Started 20 threads in the forums
answered
Senior Member

Use stripinput() on variables which are not numeric, explained here.

Use isnum() on variables which should be numeric, explained here.
0 replies
A
afif
A
afif 10
  • Member, joined since
  • Contributed 183 posts on the community forums.
  • Started 12 threads in the forums
answered
Member

Quote

googlebot wrote:
And while that solution is all well and good, it is not secure. I recommend just using the $id variable in the PHP code if you already have it set, instead of putting it into a hidden input field, because you still have access to the variable and you wouldn't be leaving it open to script manipulation.


sorry but I don't get you. when you said "using the $id variable in the PHP code", how should that be implemented? can you give an example?

Thanks in advance.
0 replies
S
starefossen
S
www.postexus.com - Follow Postexus on Facebook.
  • Senior Member, joined since
  • Contributed 359 posts on the community forums.
  • Started 20 threads in the forums
answered
Senior Member

I'll try to explain how script manipulation works.

Imagine you have this form:

$id = (isnum($_GET['id']) ? $_GET['id'] : "1");
echo "<form action='".FUSION_SELF."?id=".$id."' method='post'>\n";
echo "<input type='hidden' name='id' value='".$id."' />\n";
echo "<input type='text' class='textbox' name='text' value='Type some text here' />\n";
echo "<input type='submit' class='button' name='submit' value='Send' />\n";
echo "</form>\n";



You submit the form and this is the code making the input to the database:
if (isset($_POST['submit']) && isnum($_POST['id'])) {
$text = stripinput($_POST['text']);
$result = dbquery("UPDATE ".DB_TABLE." SET text='".$text."' WHERE id='".$_POST['id']."'");
redirect(FUSION_SELF."?id=".$_POST['id']);
}



As the $_POST['id'] is a hidden input field in the form it can be altered the same way the $_POST['text'] field can, simply edit the source code and you can alter the value of the hidden field to any value:
<input type='hidden' name='id' value='2' />\n


As you can see, the form will submit $_POST['id'] == 2; which is not what we want, as id for this item is 1, the item with id == 2 might have restricted access for the user or might not even exist. If it has restricted access this will allow the hacker to insert data to an item he should not be able to insert data to.

There are several ways to prevent such thing from happening, the easiest way is not relying on hidden fields but using the $_GET['id'] in stead like this:


// We check if the ID is numeric, if not we send them back
if (!isset($_GET['id']) || !isnum($_GET['id'])) { redirect("../index.php"); }

// Check if item exists and that the user have access to it
$result = dbquery("SELECT id, text FROM ".DB_TABLE." WHERE id='".$_GET['id']."' AND ".groupaccess('access')." LIMIT 1");

if (dbrows($result) {
// We can now safely use the $_GET['id'] as we know it exists in the DB and the user have access to the item

if (isset($_POST['submit'])) {
$text = stripinput($_POST['text']);
$result = dbquery("UPDATE ".DB_TABLE." SET text='".$text."' WHERE id='".$_GET['id']."'");
redirect(FUSION_SELF."?id=".$_GET['id']);
}

$data = dbarray($result);

echo "<form action='".FUSION_SELF."?id=".$_GET['id']."' method='post'>\n";
echo "<input type='text' class='textbox' name='text' value='".$data['text']."' />\n";
echo "<input type='submit' class='button' name='submit' value='Send' />\n";
echo "</form>\n";
} else {
echo "Item does not exists or you do not have access to it.";
}


You can read more about the groupaccess() function here.
0 replies
C
Craig
C
Craig 14
  • Fusioneer, joined since
  • Contributed 4,462 posts on the community forums.
  • Started 212 threads in the forums
answered
Fusioneer

Great answer Starefossen. ;)
0 replies

Category Forum

General Discussion

Labels

None yet

Statistics

  • Views 0 views
  • Posts 10 posts
  • Votes 0 votes
  • Topic users 9 members

9 participants

M
M
  • Senior Member, joined since
  • Contributed 267 posts on the community forums.
  • Started 4 threads in the forums
C
C
Craig 14
  • Fusioneer, joined since
  • Contributed 4,462 posts on the community forums.
  • Started 212 threads in the forums
S
S
www.postexus.com - Follow Postexus on Facebook.
  • Senior Member, joined since
  • Contributed 359 posts on the community forums.
  • Started 20 threads in the forums
A
A
Arda 10
  • Member, joined since
  • Contributed 150 posts on the community forums.
  • Started 11 threads in the forums
A
A
afif 10
  • Member, joined since
  • Contributed 183 posts on the community forums.
  • Started 12 threads in the forums
B
B
Basti 10
[PHP-Fusion Crew Member & Admin from June 2008 - December 2010]

http://basti2web.de - Support Site for my infusions
  • Veteran Member, joined since
  • Contributed 1,099 posts on the community forums.
  • Started 32 threads in the forums
G
G
Visit the new home of the merge between Hacking Vs. Security and Security Override!
My copyright removal has been switched over from HvS to SecurityOverride.
  • Senior Member, joined since
  • Contributed 638 posts on the community forums.
  • Started 28 threads in the forums
P
P
  • Veteran Member, joined since
  • Contributed 1,633 posts on the community forums.
  • Started 29 threads in the forums
W
W
  • Junior Member, joined since
  • Contributed 22 posts on the community forums.
  • Started 11 threads in the forums
  • Started this discussions

Notifications

Track thread

You are not receiving notifications from this thread.

Related Questions

Not yet