Vue.js Simple Login with PHP/MySQLi

Getting Started

I've use Bootstrap CDN in this tutorial, so you need internet connection for it to work.

Creating our Database

1. Open phpMyAdmin. 2. Click databases, create a database and name it as vuelogin. 3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.
  1. CREATE TABLE `user` (
  2. `userid` INT(11) NOT NULL AUTO_INCREMENT,
  3. `username` VARCHAR(30) NOT NULL,
  4. `password` VARCHAR(30) NOT NULL,
  5. PRIMARY KEY(`userid`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
database sql

Inserting Data into our Database

Next. we're gonna insert sample data into our database. These are our sample users that you can use to log into the system. 1. Click database vuelogin that we have created earlier. 2. Click SQL and paste the ff codes:
  1. INSERT INTO `user` (`userid`, `username`, `password`) VALUES
  2. (1, 'nurhodelta', 'neovic'),
  3. (2, 'gemalyn', 'cepe');
Username: nurhodelta Password: neovic Username: gemalyn Password: cepe

index.php

This is our index which contains our login form.
  1. <?php
  2. if(isset($_SESSION['user'])){
  3. header('location:success.php');
  4. }
  5. ?>
  6. <!DOCTYPE html>
  7. <html>
  8. <head>
  9. <title>Vue.js Login with PHP/MySQLi</title>
  10. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  11. </head>
  12. <body>
  13. <div class="container">
  14. <h1 class="page-header text-center">Vue.js Login with PHP/MySQLi</h1>
  15. <div id="login">
  16. <div class="col-md-4 col-md-offset-4">
  17.  
  18. <div class="panel panel-primary">
  19. <div class="panel-heading"><span class="glyphicon glyphicon-lock"></span> Sign in</div>
  20. <div class="panel-body">
  21. <label>Username:</label>
  22. <input type="text" class="form-control" v-model="logDetails.username" v-on:keyup="keymonitor">
  23. <label>Password:</label>
  24. <input type="password" class="form-control" v-model="logDetails.password" v-on:keyup="keymonitor">
  25. </div>
  26. <div class="panel-footer">
  27. <button class="btn btn-primary btn-block" @click="checkLogin();"><span class="glyphicon glyphicon-log-in"></span> Login</button>
  28. </div>
  29. </div>
  30.  
  31. <div class="alert alert-danger text-center" v-if="errorMessage">
  32. <button type="button" class="close" @click="clearMessage();"><span aria-hidden="true">&times;</span></button>
  33. <span class="glyphicon glyphicon-alert"></span> {{ errorMessage }}
  34. </div>
  35.  
  36. <div class="alert alert-success text-center" v-if="successMessage">
  37. <button type="button" class="close" @click="clearMessage();"><span aria-hidden="true">&times;</span></button>
  38. <span class="glyphicon glyphicon-check"></span> {{ successMessage }}
  39. </div>
  40.  
  41. </div>
  42. </div>
  43. </div>
  44. <script src="vue.js"></script>
  45. <script src="axios.js"></script>
  46. <script src="app.js"></script>
  47. </body>
  48. </html>

app.js

This contains our Vue.js codes.
  1. var app = new Vue({
  2. el: '#login',
  3. data:{
  4. successMessage: "",
  5. errorMessage: "",
  6. logDetails: {username: '', password: ''},
  7. },
  8.  
  9. methods:{
  10. keymonitor: function(event) {
  11. if(event.key == "Enter"){
  12. app.checkLogin();
  13. }
  14. },
  15.  
  16. checkLogin: function(){
  17. var logForm = app.toFormData(app.logDetails);
  18. axios.post('login.php', logForm)
  19. .then(function(response){
  20.  
  21. if(response.data.error){
  22. app.errorMessage = response.data.message;
  23. }
  24. else{
  25. app.successMessage = response.data.message;
  26. app.logDetails = {username: '', password:''};
  27. setTimeout(function(){
  28. window.location.href="success.php";
  29. },2000);
  30.  
  31. }
  32. });
  33. },
  34.  
  35. toFormData: function(obj){
  36. var form_data = new FormData();
  37. for(var key in obj){
  38. form_data.append(key, obj[key]);
  39. }
  40. return form_data;
  41. },
  42.  
  43. clearMessage: function(){
  44. app.errorMessage = '';
  45. app.successMessage = '';
  46. }
  47.  
  48. }
  49. });

login.php

This is our login API written in PHP.
  1. <?php
  2.  
  3. $conn = new mysqli("localhost", "root", "", "vuelogin");
  4.  
  5. if ($conn->connect_error) {
  6. die("Connection failed: " . $conn->connect_error);
  7. }
  8.  
  9. $out = array('error' => false);
  10.  
  11. $username = $_POST['username'];
  12. $password = $_POST['password'];
  13.  
  14. if($username==''){
  15. $out['error'] = true;
  16. $out['message'] = "Username is required";
  17. }
  18. else if($password==''){
  19. $out['error'] = true;
  20. $out['message'] = "Password is required";
  21. }
  22. else{
  23. $sql = "select * from user where username='$username' and password='$password'";
  24. $query = $conn->query($sql);
  25.  
  26. if($query->num_rows>0){
  27. $row=$query->fetch_array();
  28. $_SESSION['user']=$row['userid'];
  29. $out['message'] = "Login Successful";
  30. }
  31. else{
  32. $out['error'] = true;
  33. $out['message'] = "Login Failed. User not Found";
  34. }
  35. }
  36.  
  37.  
  38.  
  39. $conn->close();
  40.  
  41. header("Content-type: application/json");
  42. echo json_encode($out);
  43. die();
  44.  
  45.  
  46. ?>

success.php

This is our goto page after a successful login.
  1. <?php
  2. $conn = new mysqli("localhost", "root", "", "vuelogin");
  3.  
  4. if ($conn->connect_error) {
  5. die("Connection failed: " . $conn->connect_error);
  6. }
  7.  
  8. if (!isset($_SESSION['user']) ||(trim ($_SESSION['user']) == '')){
  9. header('location:index.php');
  10. }
  11.  
  12. $sql="select * from user where userid='".$_SESSION['user']."'";
  13. $query=$conn->query($sql);
  14. $row=$query->fetch_array();
  15.  
  16. ?>
  17. <!DOCTYPE html>
  18. <html>
  19. <head>
  20. <title>Vue.js Login with PHP/MySQLi</title>
  21. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  22. </head>
  23. <body>
  24. <div class="container">
  25. <div class="jumbotron">
  26. <h1 class="text-center">Welcome, <?php echo $row['username']; ?>!</h1>
  27. <a href="logout.php" class="btn btn-primary"><span class="glyphicon glyphicon-log-out"></span> Logout</a>
  28. </div>
  29. </div>
  30. </body>
  31. </html>

logout.php

Lastly, this is our logout that destroys our user session.
  1. <?php
  2. header('location:index.php');
  3. ?>
That ends this tutorial. Happy Coding :)

Comments

Submitted byAnonymous (not verified)on Mon, 04/09/2018 - 00:35

its very easy to understand the codes and make the necessary amendments

Add new comment