Advertisement
Guest User

Untitled

a guest
Jun 30th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. $usererr = $pwderr = $nameerr = $emailerr = "";
  5. $user = $pwd = $name = $email = "";
  6. $exec = FALSE ;
  7. if ($_SERVER["REQUEST_METHOD"] == "POST"){
  8. if (empty($_POST["username"])) {
  9. $usererr = "Userame is required";
  10. $exec = FALSE;
  11. }
  12. else {
  13. $user = test_input($_POST["username"]);
  14. if (strlen($user) < 5){
  15. $usererr = "Username should be between 5 to 10 characters";
  16. $exec = FALSE;
  17. }
  18. elseif(strlen($user) > 10){
  19. $usererr = "Username should be between 5 to 10 characters";
  20. $exec = FALSE;
  21. }
  22. }
  23.  
  24. if (empty($_POST["password"])) {
  25. $pwderr = "Password is required";
  26. $exec = FALSE;
  27. }
  28. else {
  29. $pwd = test_input($_POST["password"]);
  30.  
  31. if(!preg_match('/^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{5,}$/', $pwd)){
  32. $pwderr="Password should be at least 5 characters long with one letter, digit and special character";
  33. $exec = FALSE;
  34. }
  35. $pwd = sha1($pwd);
  36. }
  37.  
  38. if (empty($_POST["name"])) {
  39. $nameerr = "Full name is required";
  40. $exec = FALSE;
  41. }
  42. else {
  43. $name = test_input($_POST["name"]);
  44. }
  45.  
  46. if (empty($_POST["email"])) {
  47. $emailerr = "Email is required";
  48. $exec = FALSE;
  49. }
  50. else {
  51. $email = test_input($_POST["email"]);
  52. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  53. $emailerr = "Invalid email format";
  54. $exec = FALSE;
  55. }
  56. }
  57. if($exec)
  58. {
  59. $SERVER = 'localhost:9000';
  60. $USERNAME = 'root';
  61. $PASSWORD = 'password';
  62. $DATABASE = 'Task3';
  63. //$PORT = 9000
  64. $conn = new mysqli($SERVER,$USERNAME,$PASSWORD,$DATABASE);
  65.  
  66. if( $conn->connect_error ) {
  67. die('Could not connect: ' . $conn->connect_error);
  68. }
  69.  
  70.  
  71.  
  72. $sql = "INSERT INTO Table1 (username, password, fullname,
  73. email) VALUES ('$user', '$pwd', '$name', '$email')";
  74.  
  75. /*if ($stm = $conn->prepare("INSERT INTO Table1 (username, password, fullname, email) VALUES ( ?, ?, ?, ?)"))
  76. {
  77. $stm->bind_param("sssssss", $user, $pwd, $name, $email); //preventing sql injection
  78.  
  79.  
  80. if($stm->execute() == TRUE)
  81. {
  82. echo " New record created successfully " ;
  83. }
  84.  
  85. else
  86. {
  87. echo "Error: " . $conn->error;
  88. }
  89. stm->close(); }*/
  90.  
  91.  
  92. $conn->close();
  93. }
  94. }
  95. function test_input($data) {
  96. $data = trim($data);
  97. $data = stripslashes($data);
  98. $data = htmlspecialchars($data);
  99. return $data;
  100. }
  101.  
  102.  
  103.  
  104. ?>
  105.  
  106. <htmllang="en-IN">
  107. <head>
  108. <title>Task3</title>
  109. <style>
  110. .error {color: #FF0000;}
  111. </style>
  112. </head>
  113.  
  114. <body>
  115.  
  116.  
  117. <h1>Test Form</h1>
  118. <p><span class="error">* required </span></p>
  119. <form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  120. Username: <input type="text" name="username" value="<?php echo $user;?>"/>
  121. <span class="error">* <?php echo $usererr;?></span><br><br>
  122. Password: <input type="text" name="password"value="<?php echo $pwd;?>" />
  123. <span class="error">* <?php echo $pwderr;?></span><br><br>
  124. Full Name: <input type="text" name="name"value="<?php echo $name;?>" />
  125. <span class="error">* <?php echo $nameerr;?></span><br><br>
  126. E-mail address: <input type="text" name="email" value="<?php echo $email;?>"/>
  127. <span class="error">* <?php echo $emailerr;?></span><br><br>
  128. <input type="submit" value="Submit">
  129. </form>
  130. </body>
  131. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement