4 Replies - 250 Views - Last Post: 25 March 2019 - 11:44 AM Rate Topic: -----

#1 rainbowskittles   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-March 19

How do I make a php login page actually work?

Posted 24 March 2019 - 12:21 PM

So this is really starting to get on my nerves. All I am trying to do is create a login page using php and pgadmin postgresql.

I have the form made and it looks pretty good but I cant get the user to login and display a welcome message. I have changed this code so many times i dont even know what to do

<?php
include 'header.php'
?>


<?php
   include("functions.php");
   session_start();

   if($_SERVER["REQUEST_METHOD"] == "POST") {
      // username and password sent from form

      $myusername = pg_real_escape_string($db,$_POST['username']);
      $mypassword = pg_real_escape_string($db,$_POST['password']);

      $sql = "SELECT username FROM users WHERE username = '$myusername' and passcode = '$mypassword'";
      $result = pg_query($db,$sql);
      $row = pg_fetch_array($result,MYSQLI_ASSOC);
      $active = $row['active'];

      $count = pg_num_rows($result);

      // If result matched $myusername and $mypassword, table row must be 1 row

      if($count == 1) {
         session_register("myusername");
         $_SESSION['login_user'] = $myusername;

         header("location: welcome.php");
      }else {
         $error = "Your Login Name or Password is invalid";
      }
   }
?>

<html>

   <head>
      <title>Login Page</title>
   </head>

<center>
<h2>Please log in</h2>
<p>Enter your login ID and password to connect to this system<br/>
</p>
<div align = "center">
				<div>

					 <div style = "margin:30px">

							<form action = "" method = "post">
								<table border = "1">
								 <td><label>Username  :</label><input type = "text" name = "username" class = "box" placeholder="Username"/><br /><br /></td>
								 <td><label>Password  :</label><input type = "password" name = "password" class = "box" placeholder="**********"/><br /><br /></td>
								 <td><input type = "submit" value = " Submit "/><br /></td>
							 </table>
							</form>

							<div><?php echo $error; ?></div>

					 </div>

				</div>

		 </div>

<p>
Please wait after pressing <strong>Log in</strong>
while we retrieve your records from our database.<br/>
<em>(This may take a few moments)</em>
</p>
<hr/>
</body>
</html>


This is my current code any ideas???

Is This A Good Question/Topic? 0
  • +

Replies To: How do I make a php login page actually work?

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3813
  • View blog
  • Posts: 13,857
  • Joined: 08-August 08

Re: How do I make a php login page actually work?

Posted 24 March 2019 - 04:04 PM

Deprecated MySQL functions are simpler to use (not counting all the sanitizing you need to add) than PDO or MySQLi prepared statements, but far less secure. That's why they've been deprecated. If you're just learning PHP you should start with
http://www.dreaminco...duction-to-pdo/
or
http://php.net/manua...-statements.php
When dealing with old code that uses deprecated MySQL functions you should replace them if possible. If it's not possible, your experience with PDO or MySQLi will benefit you when dealing with MySQL functions.

Two additional points:
  • The MySQL extension is no longer deprecated. It has been removed from PHP starting with version 7.0.
  • I stop reading when I see code with user supplied data entered into a query, and I encourage others to do the same. Prepared statements are that important.

Was This Post Helpful? 0
  • +
  • -

#3 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5378
  • View blog
  • Posts: 14,286
  • Joined: 18-April 07

Re: How do I make a php login page actually work?

Posted 24 March 2019 - 05:49 PM

I have made a blog post about this a while back. Here is the post to show you how I set it up.

https://www.codersle...script-example/

I hope it helps. :)
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6777
  • View blog
  • Posts: 27,959
  • Joined: 12-December 12

Re: How do I make a php login page actually work?

Posted 25 March 2019 - 02:33 AM

You should move session_start to the top of the code.

Note that you are using a number of tags and attributes that are obsolete or no longer exist, center, align, border. Use modern CSS.

You should be using prepared statements, you are open to SQL injection.
Was This Post Helpful? 0
  • +
  • -

#5 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2249
  • View blog
  • Posts: 6,881
  • Joined: 15-January 14

Re: How do I make a php login page actually work?

Posted 25 March 2019 - 11:44 AM

You need to use prepared statements instead of putting variables in your query:

https://www.php.net/....pg-prepare.php

You also need to hash passwords that you store in the database:

https://www.php.net/...ssword-hash.php

And verify:

https://www.php.net/...word-verify.php

Make sure you have all error messages enabled, with the code above you would get an error when you try to use session_start, it wouldn't work. It needs to be before any output. Also, you don't need to use session_register, just start the session then put whatever you want in $_SESSION.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1