homepage Welcome to WebmasterWorld Guest from 54.237.184.242
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP Password protected PHP Forum
Seedy




msg:4435892
 5:01 pm on Apr 1, 2012 (gmt 0)

I have created a php form which submits data into an sql database and I have tried to protect the page using a simple php password login - which upon successfull login displays the database form using an include('');.
The database entry form and the login both work well independently but together, once logged in, the data form does not submit the data - presumably as there are then two forms present in the one page.

What might I need to do in order for the second form to submit the data to the database, while logged in? Would setting a cookie help? Should I rethink the log method?

Any advice appreciated.

page1.php
<?php
if (isset($_POST["password"]) && ($_POST["password"]=="$password")) {
?>
<!-- START OF HIDDEN HTML -->
<p align="center"><br><br><br>
<b>Access Gained</b></p>
<?php
include('page2.php');
?>
<!-- END OF HIDDEN HTML -->
<?php
}
else
{
if (isset($_POST['password']) || $password == "") {
print "<p align=\"center\"><font color=\"red\"><b>Incorrect Password</b></font></p>";}
print "<form method=\"post\"><p align=\"center\">Enter password<br>";
print "<input name=\"password\" type=\"password\" size=\"25\" maxlength=\"10\"><input value=\"Login\" type=\"submit\"></p></form>";
}
?>


page2.php
<?php
if (isset($_REQUEST['Submit'])) {
$sql = "INSERT INTO $db_table(data) values ('".stripslashes($_REQUEST['data'])."')";
if($result = mysql_query($sql ,$db)) {
echo 'Data Submitted';
} else {
echo "ERROR: ".mysql_error();
}
} else {
echo '<form method="post" action="">';
echo '<table width="100%">';
echo '<tr><td class="scores">Data:<br /><input type="text" name="data" value="" /></td></tr>';
echo '<tr><td class="scores"><input type="submit" name="Submit" value="Submit"><input type="reset" name="reset" value="Reset"></td></tr>';
echo '</table></form>';
}
?>

 

cffrost2




msg:4436248
 3:55 pm on Apr 2, 2012 (gmt 0)

What if you set a session var upon success and not show the password form if the var is set. Then you'll only have one form show on the page. Just a thought.


<?php
if (isset($_POST["password"]) && ($_POST["password"]=="$password")) {
$_SESSION['granted'] == 'grant';
}
elseif (isset($_POST['password']) || $password == "") {
print "<p align=\"center\"><font color=\"red\"><b>Incorrect Password</b></font></p>";
print "<form method=\"post\"><p align=\"center\">Enter password<br>";
print "<input name=\"password\" type=\"password\" size=\"25\" maxlength=\"10\"><input value=\"Login\" type=\"submit\"></p></form>";
}
if($_SESSION['granted'])
{
?>
<!-- START OF HIDDEN HTML -->
<p align="center"><br><br><br>
<b>Access Gained</b></p>
<?php
include('page2.php');
?>
<!-- END OF HIDDEN HTML -->
<?php
}
else
{
print "<form method=\"post\"><p align=\"center\">Enter password<br>";
print "<input name=\"password\" type=\"password\" size=\"25\" maxlength=\"10\"><input value=\"Login\" type=\"submit\"></p></form>";
}
?>


Hope this helps.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved