Simple login system with php

EDIT: it works now, thanks people.
I created a simple login system with php, after creating some users in MySQL. But when I try to log in, I enter a completely blank page, no matter what my login is. Here is my html ...

<body> <div class="mainbox"> <h3 align="center">Log In</h3> <hr> <form role="form" class="form-horizontal" method="POST" action="connectivity.php"> <div class="form-group"> <label for="Username">Username:</label> <input type="text" class="form-control" name="username" size="40" placeholder="Enter Username"> </div> <div class="form-group"> <label for="pwd">Password:</label> <input type="password" class="form-control" name="pass" size="40" placeholder="Enter Password"> </div> <button id="Submit" type="submit" name="submit" class="btn btn-default">Submit</button> </form> </div> </body> 

and here connectivity.php ...

 <?php define('DB_HOST', 'localhost'); define('DB_NAME', 'simplelogin'); define('DB_USER','root'); define('DB_PASSWORD',''); $con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error()); $db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error()); function SignIn() { session_start(); if(!empty($_POST['username'])) { $query = mysql_query("SELECT * FROM UserName where userName = '$_POST[username]' AND pass = '$_POST[pass]'") or die(mysql_error()); $row = mysql_fetch_array($query) or die(mysql_error()); if(!empty($row['userName']) AND !empty($row['pass'])) { $_SESSION['userName'] = $row['pass']; echo "Successful login to user profile page!"; } else { echo "Sorry, You have either entered a wrong Username or a wrong password. Please retry"; } } } if(isset($_POST['submit'])) { SignIn(); } ?> 

Function not executing, what should I do?

+6
source share
4 answers

At first . You should always use the POST method to log in. POST method is safe, not GET . POST does not provide information through the URL. For more information, to gain knowledge, the difference between GET and POST go to Link .

Second one . You should avoid the mysql_* methods. For information, go to SO LInk .

Third . To submit a form in which you use the <button tag, and there is no JS to submit the form to your click, then your form will not be submitted. also did not define its type as submit as `type =" submit ". It is better to use the input type submit to submit the form.

Fourth . You get form data using the POST method and in the form using the GET method. Thus, your action file cannot receive data due to a method conflict. Change the method in the tag as method="post" .

Fifth , the session should be running at the top of the page.

Sixth . It is best to validate the form with validation on the client side, and then validation on the server side should also be applied.

Now in your code:

  • change the attribute of the <form> as method="post" .
  • Use <input type="submit" name="submit" value="Submit" /> to submit the form. the action file will receive data from the form using the element name and you are missing in input type="submit" name="submit"
  • Remove mysql_* functions from your code and use mysqli_* to connect to the database and use it later.
+10
source

Your form has a GET method, but your link expects a POST. Change the method in your form to POST, and it should be better.

Also, you do not have a name attribute on the submit button. If you post a form, it will look for attributes of the name, not identifiers. That way, it will never go into your function.

+4
source

You must specify the name of the button, for example, "send." Try replacing the button code as shown below.

 <button id="Submit" name="submit" type="submit" class="btn btn-default">Submit</button> 

It will work as your exception .. :-)

+4
source

you also need

 <input id="Submit" type="submit" name="submit" class="btn btn-default">Submit</button> 
+3
source

Source: https://habr.com/ru/post/988973/


All Articles