Guest User

Untitled

a guest
Mar 25th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. session_start();
  5. $username = $_POST['username'];
  6. $password =$_POST['password'];
  7. $email = $_POST ['email'];
  8. $phone = $_POST['phone'];
  9.  
  10.  
  11.  
  12. if(!empty ($username) && !empty($email)){
  13. if(!empty ($password)){
  14.  
  15.  
  16.  
  17. $host = "host";
  18. $dbusername = "user";
  19. $dbpassword = "*****";
  20. $dbname = "dbname";
  21.  
  22. $conn = new mysqli ($host,$dbusername,$dbpassword,$dbname);
  23.  
  24. if(mysqli_connect_error()){
  25. die('Connect Error ('. mysqli_connect_errno().')' . mysqli_connect_error());
  26. }
  27.  
  28.  
  29.  
  30. else{
  31.  
  32. $username= mysqli_real_escape_string($conn,$_POST["username"]);
  33. $email= mysqli_real_escape_string($conn,$_POST["email"]);
  34. $phone= mysqli_real_escape_string($conn,$_POST["phone"]);
  35. $password= mysqli_real_escape_string($conn,$_POST["password"]);
  36. $hpassword = password_hash($password,PASSWORD_DEFAULT);
  37.  
  38. $sql = "INSERT INTO user(username,password,email,phone) values('$username','$hpassword','$email','$phone')";
  39. if($conn->query($sql)){
  40. $_SESSION['username'] = $username;
  41. $_SESSION['success'] = "You are now logged in";
  42. header('location: index.php');
  43. }
  44. else{
  45. echo "error: ". $sql."<br>".$conn->error;
  46. }
  47. $conn->close();
  48. }
  49.  
  50.  
  51.  
  52. }
  53. else {
  54. echo "Password should not be empty";
  55. //echo "<script>alert('Password should not be empty!');</script>";
  56. die();
  57. }
  58. }
  59.  
  60. else{
  61. echo "Username/Email should not be empty";
  62. die();
  63. }
  64.  
  65.  
  66.  
  67. ?>
  68.  
  69. <?php
  70. session_start();
  71.  
  72. $error='';//here we store potential errors
  73.  
  74. if(isset($_POST['submit'])) {
  75. if(empty($_POST['username']) || empty($_POST['password'])) {
  76. // echo $error = "Fill in all fields!";
  77.  
  78. echo "<script>alert('Fill in all fields!');</script>";
  79. // header("Location: index.php");
  80. //why it redirects me to empty page???
  81. }
  82. else
  83. {
  84.  
  85. //define the variables
  86. $username= mysqli_real_escape_string($conn,$_POST["username"]);
  87. $password= mysqli_real_escape_string($conn,$_POST["password"]);
  88.  
  89. //connection
  90. $conn= mysqli_connect("host","user","******","dbname");
  91.  
  92. /* check connection */
  93. if ( mysqli_connect_errno() ) {
  94. printf("Connect failed: %sn", mysqli_connect_error());
  95. exit();
  96. }
  97.  
  98.  
  99.  
  100. if( $stmt = $conn->prepare("SELECT username FROM user WHERE username = ? AND password = ? ") )
  101. {
  102. $stmt->bind_param("ss",$username,$password);
  103. $stmt->execute();
  104.  
  105. $stmt->bind_result($username);
  106.  
  107.  
  108. if($stmt->fetch())
  109. {
  110.  
  111.  
  112.  
  113. $_SESSION['username'] = $username;
  114. header("Location: index.php?success");
  115.  
  116. }
  117. else{
  118. echo "There is no such user!";
  119. header("Location: index.php?invalid");
  120. }
  121. mysqli_close($conn);
  122. }
  123. }
  124. }
  125.  
  126. ?>
Add Comment
Please, Sign In to add comment