2

I cant register/add new user in my project when i added a domain , but it is working when im only using xampp/localhost. there is a connection because i can login using an admin user and all the user that already registered in my database before i import it on 000webhost database and there is a connection because im able to login so im thinking that theres something i should change with my registration code. Please help im only a beginner

this is my code:

<?php
if(isset($_POST['submit'])){
$username = $_POST['username'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$password = $_POST['password'];

if(!empty($username) && !empty($email) && !empty($password)){
    $username = mysqli_real_escape_string($connection,$username);
    $firstname = mysqli_real_escape_string($connection,$firstname);
    $lastname = mysqli_real_escape_string($connection,$lastname);
    $address = mysqli_real_escape_string($connection,$address);
    $phone = mysqli_real_escape_string($connection,$phone);
    $email = mysqli_real_escape_string($connection,$email);
    $password = mysqli_real_escape_string($connection,$password);

    $password = password_hash($password, PASSWORD_BCRYPT, array('cost' => 12)); 

    // $query = "INSERT INTO users (user_role,username,user_firstname,user_lastname,user_email,user_password) ";
    // $query .= "VALUES( 'subscriber','{$username}','{$firstname}','{$lastname}','{$email}','{$password}' )";
    // $register_user_query = mysqli_query($connection,$query);
    // if(!$register_user_query){
    //     die("QUERY FAILED" . mysqli_error($connection) . ' ' . mysqli_errno($connection));
    // }

    $query = "INSERT INTO users (user_role,username,user_firstname,user_lastname,user_address,user_phone,user_email,user_password) ";
    $query .= "VALUES( 'subscriber',?,?,?,?,?,?,?)";
    $stmt = mysqli_prepare($connection,$query);
    mysqli_stmt_bind_param($stmt,"sssssss",$username,$firstname,$lastname,$address,$phone,$email,$password);
    mysqli_stmt_execute($stmt);
    mysqli_stmt_close($stmt);
    if(!$stmt){
        die("QUERY FAILED" . mysqli_error($connection) . ' ' . mysqli_errno($connection));
    }

    $message = "Your Registration is successful";

}else{
    $message = "fields cannot be empty";
}

}else{
$message = ""; } ?>

this is my login code:

<?php include "db.php"; ?>
<?php session_start(); ?>
<?php
if(isset($_POST['login'])){
    $username = $_POST['username'];
    $password = $_POST['password'];
    $username = mysqli_real_escape_string($connection,$username);
    $password = mysqli_real_escape_string($connection,$password);
    $query = "SELECT * FROM users WHERE username = '{$username}' ";
    $login_user_query = mysqli_query($connection,$query);
    if(!$login_user_query){
        die("QUERY FAILED " . mysqli_error($connection)); 
    }
    while($row = mysqli_fetch_assoc($login_user_query)){
        $db_user_id = $row['user_id'];
        $db_username = $row['username'];
        $db_user_firstname = $row['user_firstname'];
        $db_user_lastname = $row['user_lastname'];
        $db_user_password = $row['user_password'];
        $db_user_role = $row['user_role']; }
        if(password_verify($password,$db_user_password)){
            $_SESSION['username'] = $db_username;
            $_SESSION['user_firstname'] = $db_user_firstname;
            $_SESSION['user_lastname'] = $db_user_lastname;
            $_SESSION['user_role'] = $db_user_role;
            // header("location: ../admin");
            header("location: userverify.php");
        } else{
            header("location: ../index.php");
        } 
    } 
    ?>

-- Table structure for table users

CREATE TABLE `users` (


`user_id` int(3) NOT NULL,
`username` varchar(255) NOT NULL,
`user_password` varchar(255) NOT NULL,
`user_firstname` varchar(255) NOT NULL,
`user_lastname` varchar(255) NOT NULL,
`user_address` varchar(255) NOT NULL,
`user_phone` varchar(255) NOT NULL,
`user_email` varchar(255) NOT NULL,
`user_image` text NOT NULL,
`user_role` varchar(255) NOT NULL,
`randSalt` varchar(255) NOT NULL DEFAULT 'blahblahblahh',
`token` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- -- Dumping data for table users

INSERT INTO `users` (`user_id`, `username`, `user_password`, `user_firstname`, `user_lastname`, `user_address`, `user_phone`, `user_email`, `user_image`, `user_role`, `randSalt`, `token`)

Rmar
  • 23
  • 5
  • 2
    Get rid of the escaping code. You don't use that (or need it) when using prepared statements and parameterizing. That can cause issues as well when verifying if any quotes were present. Can you add login code as well? – user3783243 Dec 05 '20 at 13:44
  • It is a very bad idea to use `die(mysqli_error($$conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Dec 05 '20 at 13:45
  • @user3783243 i already added my login code there can u tell me whats wrong im new in this – Rmar Dec 05 '20 at 14:03
  • @Dharmanill keep that in mind thanks – Rmar Dec 05 '20 at 14:04
  • Shouldn't have all the closing and opening PHP tags. What happens with the code, session isn't set, redirection to `index.php`, or other? I've fixed indentation if this is full code `if(isset($_POST['login'])){` is never closed. – user3783243 Dec 05 '20 at 14:07
  • @user3783243 yes it will redirect to index.php which is the login page – Rmar Dec 05 '20 at 14:11
  • @user3783243 can u send it to me in email of the edited version of what should i do ? its for our thesis and i need to finish it by next week idk what to do @.@ – Rmar Dec 05 '20 at 14:15
  • Check that hash and DB values are the same, 1. Create new user, prior to `mysqli_stmt_bind_param(` line add `var_dump($password)` 2. Go to login page and run `var_dump($db_user_password)` after the `select`. – user3783243 Dec 05 '20 at 14:23
  • @user3783243 mysqli_stmt_bind_param($stmt,"sssssss",$username,$firstname,$lastname,$address,$phone,$email,$password); var_dump($password); like this? – Rmar Dec 05 '20 at 14:36
  • Yup, should show ya the hash there. – user3783243 Dec 05 '20 at 14:41
  • @user3783243 it says string(60) "$2y$12$9R41iV36vGlxYL9ImhQbjOPKc/8CrEFpZkNNFKfqp9u1EXxfso/W6" when im trying to register – Rmar Dec 05 '20 at 14:45
  • Okay, and what does the `select` return for DB value? – user3783243 Dec 05 '20 at 14:46
  • @user3783243 what do you mean `select` ohh i see you want me to try to register using localhost/xampp only? – Rmar Dec 05 '20 at 14:50
  • On domain `insert` and then `select`. Do hashes match? – user3783243 Dec 05 '20 at 14:52
  • @user3783243 yes they are the same. my registration is working if im only using localhost/xampp but when i added server hosting and domain it doesnt work. – Rmar Dec 05 '20 at 14:55
  • The only redirection for `index.php` is in the `else` for the `password_verify`? – user3783243 Dec 05 '20 at 15:02
  • @user3783243 this is what my verify user looks like `` ``` – Rmar Dec 05 '20 at 15:06
  • Is the code in the question not the only part at play here? – user3783243 Dec 05 '20 at 15:13
  • @user3783243 can i email you my project so you can check it ? i need help :( – Rmar Dec 05 '20 at 15:21
  • @Rmar I won't have enough time to go through a project. Post all relevant info here. e.g. table definition, stored hash, retrieved hash, relevant code – user3783243 Dec 05 '20 at 15:24
  • @user3783243 i alrd check all the relevant codes are alrd here check the post i updated it i added the table im using in my database for user registration – Rmar Dec 05 '20 at 15:40

0 Answers0