Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- $username = $_POST['username'];
- $password =$_POST['password'];
- $email = $_POST ['email'];
- $phone = $_POST['phone'];
- if(!empty ($username) && !empty($email)){
- if(!empty ($password)){
- $host = "host";
- $dbusername = "user";
- $dbpassword = "*****";
- $dbname = "dbname";
- $conn = new mysqli ($host,$dbusername,$dbpassword,$dbname);
- if(mysqli_connect_error()){
- die('Connect Error ('. mysqli_connect_errno().')' . mysqli_connect_error());
- }
- else{
- $username= mysqli_real_escape_string($conn,$_POST["username"]);
- $email= mysqli_real_escape_string($conn,$_POST["email"]);
- $phone= mysqli_real_escape_string($conn,$_POST["phone"]);
- $password= mysqli_real_escape_string($conn,$_POST["password"]);
- $hpassword = password_hash($password,PASSWORD_DEFAULT);
- $sql = "INSERT INTO user(username,password,email,phone) values('$username','$hpassword','$email','$phone')";
- if($conn->query($sql)){
- $_SESSION['username'] = $username;
- $_SESSION['success'] = "You are now logged in";
- header('location: index.php');
- }
- else{
- echo "error: ". $sql."<br>".$conn->error;
- }
- $conn->close();
- }
- }
- else {
- echo "Password should not be empty";
- //echo "<script>alert('Password should not be empty!');</script>";
- die();
- }
- }
- else{
- echo "Username/Email should not be empty";
- die();
- }
- ?>
- <?php
- session_start();
- $error='';//here we store potential errors
- if(isset($_POST['submit'])) {
- if(empty($_POST['username']) || empty($_POST['password'])) {
- // echo $error = "Fill in all fields!";
- echo "<script>alert('Fill in all fields!');</script>";
- // header("Location: index.php");
- //why it redirects me to empty page???
- }
- else
- {
- //define the variables
- $username= mysqli_real_escape_string($conn,$_POST["username"]);
- $password= mysqli_real_escape_string($conn,$_POST["password"]);
- //connection
- $conn= mysqli_connect("host","user","******","dbname");
- /* check connection */
- if ( mysqli_connect_errno() ) {
- printf("Connect failed: %sn", mysqli_connect_error());
- exit();
- }
- if( $stmt = $conn->prepare("SELECT username FROM user WHERE username = ? AND password = ? ") )
- {
- $stmt->bind_param("ss",$username,$password);
- $stmt->execute();
- $stmt->bind_result($username);
- if($stmt->fetch())
- {
- $_SESSION['username'] = $username;
- header("Location: index.php?success");
- }
- else{
- echo "There is no such user!";
- header("Location: index.php?invalid");
- }
- mysqli_close($conn);
- }
- }
- }
- ?>
Add Comment
Please, Sign In to add comment