PHP and MySQL login query

registration and login form in php and mysql
complete login system php mysql
simple php login system
simple login form in php with mysql database
login page in php with database source code
login page in php with database source code download
login system php source code
php complete login and registration system with php mysql download

I am learning MySQL and PHP and am currently having trouble with this login process.

It seems the MySQL query is not returning any rows and not redirecting me after a successful login.

PHP:

$error_msg = "";
//checks the session to see if the user is logged in
if (!isset($_SESSION['userid'])) {
    if (!isset($_POST['submit'])) {
        $dbc = mysqli_connect('localhost', 'root', '', 'login')
        or die('Error Connecting to Database on the SQL Server');
        //grabs data from POST
        $user_username = $_POST['username'];
        $user_password = $_POST['password'];
        //lookup username and password in the database
        if (!empty($user_username) && !empty($password)) {
            $query = "SELECT userid, username FROM tuser WHERE username = '$user_username' AND password = SHA('$user_password')";
            $res = mysqli_query($dbc, $query);
            //login is ok so set the user ID and username cookies, redirect to homepage
            if (mysqli_num_rows($res) == 1) {
                $row = mysqli_fetch_array($res);
                $_SESSION['userid'] = $row['userid'];
                $_SESSION['username'] = $row['username'];
                setcookie('userid', $row['userid'], time() + (60 * 60 * 24 * 30), "/");
                setcookie('username', $row['username'], time() + (60 * 60 * 24 * 30), "/");
                setcookie('email', $row['email'], time() + (60 * 60 * 24 * 30), "/");
                setcookie('password', $row['password'], time() + (60 * 60 * 24 * 30), "/");
                //redirect after successful login
                header("Location: index.php");
            } else {
                //the username and password are incorrect so set error message
                $error_msg = 'Sorry, you must enter a valid username and password to log in. <a href="Signup.php">Please sign up!</a>';
            }
        }
    }
}

HTML:

<form action="login_process.php" method="post" class="login">
 Username: <input type="text" name="username"/>
 Password: <input type="password" name="password"/>
 <input type="submit" value="submit"/>
</form>

Sorry for the sloppy code, I am familiar with PHP and MySQL and teaching myself how to do this. I made some changes and the code submits, but it does not redirect me, it just goes to login_process.php and does nothing. What am i missing? I appreciate all the help, thank you everyone!

    <?
    session_start();

    $error_msg = "";

    //checks the session to see if the user is logged in

if (!isset($_SESSION['user_id'])) 
{
    if (isset($_POST['submit'])) 
    {

        $user_username = mysql_real_escape_string($_POST['username']);
        $user_password = mysql_real_escape_string($_POST['password']);

        $dbc = mysqli_connect('localhost', 'root', '', 'login')
        or die('Error Connecting to Database on the SQL Server');
        //grabs data from POST

            //lookup username and password in the database
            if (!empty($user_username) && !empty($password)) 
            {

                $query = "SELECT userid, username FROM tuser WHERE username = '$user_username' AND password = SHA('$user_password')";

                $res = mysqli_query($dbc, $query);

                //login is ok so set the user ID and username cookies, redirect to homepage
                if (mysqli_num_rows($res) == 1) 
                {
                    $row = mysqli_fetch_array($res);

                    $_SESSION['user_id'] = $row['userid'];
                    $_SESSION['user'] = $row['username'];
                    setcookie('userid', $row['userid'], time() + (60 * 60 * 24 * 30), "/");
                    setcookie('username', $row['username'], time() + (60 * 60 * 24 * 30), "/");
                    setcookie('email', $row['email'], time() + (60 * 60 * 24 * 30), "/");
                    setcookie('password', $row['password'], time() + (60 * 60 * 24 * 30), "/");

                    //redirect after successful login
                    header("Location: home.php");
                    echo mysqli_error($dbc);

                } 
                else 
                {
                    //the username and password are incorrect so set error message
                    $error_msg = 'Sorry, you must enter a valid username and password to log in. <a href="Signup.php">Please sign up!</a>';

                    header("Location: home.php");
                    echo mysqli_error($dbc);

                }
            }
            else
            {
                $error_msg = 'Please enter a username and password to log in. <a href="Signup.php">Please sign up!</a>';

                header("Location: home.php");
                echo mysqli_error($dbc);

            }

    }
}

    ?>

I'm not sure if that's all all of your code but you must session_start() before you can access $_SESSION

Your code only queries unless a post isn't set so change it:

if (isset($_POST['submit'])) {

  //your code

}

Also, be sure to escape user input to prevent sql injection:

$user_username = mysql_real_escape_string($_POST['username']);

Make sure it's not a error in your SQL:

echo mysqli_error($dbc);

PHP - MySQL Login, PHP - MySQL Login - This tutorial demonstrates how to create a login page with MySQL Data base. Before enter into the code part, You would need special  PHP - MySQL Login. This tutorial demonstrates how to create a login page with MySQL Data base. Before enter into the code part, You would need special privileges to create or to delete a MySQL database. So assuming you have access to root user, you can create any database using mysql mysqladmin binary.

You are not executing anything the because of this line:

if (!isset($_POST['submit'])) {

It should be change to

if (isset($_POST['submit'])) {

isset() will return TRUE if that variable is set. !isset() will return TRUE if it is not set

Creating a User Login System with PHP and MySQL, Execute the following SQL query to create the users table inside your MySQL database. Example. Download. CREATE TABLE users ( id INT NOT  User authentication - login sistem code in php - is very common in modern web application. This area it is a security mechanism that is used to restrict unauthorized access to visitors and it is reserved member-only areas and tools on a site. Login form in PHP, login using PHP - how to create? - example. In this tutorial we'll create a simple

I see two problems:

Your boolean is backwards on the line (i.e. remove the ! mark)

if (!isset($_POST['submit'])) {

The second problem is that your input button lacks a name="submit" attribute, so it is never being passed, so the variable will never be present regardless of the first problem.

PHP MySQL Login Form, Login Form using PHP and MySQL​​ Well, we will use that same data and that same users table. Since we already have our table created and stored data in it: See here: Save Records, we will query that same table for what the user has input on the login form. Now let's create the login form. PHP MySQL Login System. In this tutorial you will learn how to build a login system with PHP and MySQL. Implementing User Authentication Mechanism. User authentication is very common in modern web application. It is a security mechanism that is used to restrict unauthorized access to member-only areas and tools on a site.

Thank you everyone, got it working.

<?
//Start session
session_start();

$_SESSION['message'] = "";

//Include database connection details
require_once('global.php');


//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
    die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
    die("Unable to select database");
}

if(!empty($login) && !empty($password)) {

    $login = mysql_real_escape_string($_POST['login']);
    $password = mysql_real_escape_string($_POST['password']);

    $query = "SELECT * FROM tuser WHERE username = '$login' AND password = SHA('$password')";
    $data = mysql_query($query);

    if($data) {
        if (mysql_num_rows($data) == 1 ) {
            $row = mysql_fetch_assoc($data);
            $_SESSION['userid'] = $row['userid'];
            $_SESSION['username'] = $row['username'];
            $_SESSION['message'] = "Welcome,&nbsp;" . $_SESSION['username'];
            header('Location: member.php');
            exit();
        }
        else {
            $_SESSION['message'] = "Please enter a valid username or password";
            header('Location: error.php');
            exit();
        }

    }
    else {
        die("Query failed");
    }

}
else {
    $_SESSION['message'] = "Please enter a username or password";
    header('Location: error.php');
    exit();
}


?>

Create Simple Login Page with PHP and MySQL, If query returns greater than 0 then initalize $_SESSION['uname'] = $uname and redirect to home.php . If it returns 0 then display the "Invalid  User login system tutorial using HTML, PHP, MySql, Session and CSS on which user can log in to the profile page and log out. This is a very basic but effective tutorial. Where I do not only focus what it gives but also focus on how it gives. After watching this, I hope you will learn about HTML, PHP, MySql and also Session.

How to Login with Username or Email using PHP & MYSQL, How to login with username or email in PHP & MYSQL. April 7, 2020 by Karthikeyan K Email Id or it returns false. Based on this we will easily switch the query  This is a Simple to Advanced Login Script System using PHP and MySQL. In this script, a form will be displayed with two fields, username, and password. When the user is submitting with valid username and password, then he can access authenticated page. Otherwise, users again have to fill in the form.

Admin and user login in php and mysql database, All users (Admins as well as normal users) use the same form to login. Recommended Udemy course: The Complete PHP MySQL Professional Course with 5 'user', '$password')"; mysqli_query($db, $query); // get id of the created user  PHP | MySQL Select Query. The SQL SELECT statement is used to select the records from database tables. Syntax : To select all columns from the table, the character is used. Implementation of the Select Query : Let us consider the following table ‘ Data ‘ with three columns ‘ FirstName ‘, ‘ LastName ‘ and ‘ Age ‘.

Login System with PHP and MySQL Database with session, How do I create a secure login script in PHP and MySQL? If you need to execute sevaral SQL commands in a row (usually called batcg SQL) using PHP you canot use mysql_query() since it can execute single command only. Here is simple but effective function that can run batch SQL commands.

Comments
  • Start SQL injection comments... now...!
  • Try to echo the username and password before you initialize the connection to the database. to determine if you code actually entered your if statement. since i am having some second thoughts on your $_POST['submit']. seems to be wrong.
  • Oh I agree, indenting your code so it's readable is totally overrated.
  • your submit should be if (isset($_POST['submit']))...start there
  • @Ronnie You mean you think he should actually execute some code? Radical idea, I'm not sure if that will catch on...
  • It's actually session_start :P