Jump to content

Newbie code


wwprnama

Recommended Posts

I hope I don't get flamed for this, cause I figured this might be the forum to post this in.  Anyways, I'm starting to learn php and I got this code I was testing :

 

<body>

 

<h1>Hi User</h1>

 

<?php

 

if (empty($userName)) {

print "

<form method=\"post\" action=\"one page form.php\">

Please enter your name :

<input type = \"text\"

name = \"userName\"><br>

<input type = \"submit\">

</form>";}

else

{

$userName = $_REQUEST["userName"];

print "<h3>Hi there, $userName!</h3>";};

?>

 

</body>

 

 

Now as  you can see, the attempt here is to get a form value on the same page with that empty function.  So with that being said, any tips or pieces of advice one could offer me?

Link to comment
Share on other sites

First of all wrong forum use php code forum. beta test your stuff is for functional products that can be linked to for SQL injection, XSS, CSRF, and other vulnerability and functional testing.

 

Secondly the code is poorly written. AVOID $_REQUEST which allows GET, POST, and COOKIE a real security threat.

 

<?php
if (empty($_POST["userName"])) {
   print "
   <form method=\"post\" action=\"one page form.php\">
   Please enter your name :
   <input type = \"text\"
      name = \"userName\">
   <input type = \"submit\">
   </form>";
}else{
   $userName = $_POST["userName"];
   print "<h3>Hi there, $userName!</h3>";
}
   ?>

Link to comment
Share on other sites

YOu shoud use isset rather than empty:

<?php

if(isset($_POST['userName']) && !empty($_POST['userName']))
{
    $userName = $_POST['userName'];

    print "<h3>Hi there, $userName!</h3>";
}
else
{
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
   Please enter your name : <input type="text" name="userName"><br />
   <input type="submit" value="Post Username">
</form>
<?php } ?>

 

Link to comment
Share on other sites

Thanks guys,

I'm mainly learning from a book called PHP 5/MySQL Programming for the Absolute Beginner, written by Andy harris.  I'm pretty sure that alot of the code that gets thrown at me through there is error after error of poorly written code, so I can only hope that i'll find enough poeple on here that wanna help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.