Jump to content

Login script help


thokin

Recommended Posts

Hi, I'm new to PHP and I'm trying to create a login script, but it doesn't seem to be working.

So if anyone can help me by telling what's wrong or missing I would greatly appreciate it.

 

Here is my code and the problem is that when i type the username and password it just remains on the same page and doesn't move to another. I have the username and password in the database and I'm connecting to it via include connection.php:

 

<html>

<head>

<title>FreshCut </title>

<link rel="stylesheet" type="text/css" href="Styling.css" />

</head>

<body>

<h3 class="Innlogging">Login</h3>

 

<form action="Login.php" method="post" name="loginform" id="loginform">

  <p>Username:

  <input type="text" name="user">

  </p>

  <p>Password:

  <input type="password" name="pass">

  </p>

  <p>

  <input type="submit" name="Submit" value="Logg inn">

  </p>

  </form>

 

<?php

 

  $user = $_POST['user']; //pulls the username from the form

  $pw = $_POST['pass']; //pulls the pass from the form

  $pass = md5($pw); //makes our password an md5

  include("connect.php"); //connects to our mysql database

  $login = mysql_query("SELECT * FROM users WHERE username = '$user' AND password = '$pass`"); //selects info from our table if the row has the same user and pass that our form does

  if(!mysql_num_rows($login)) //if the username and pass are wrong

  {

        header("Location: Login.php");  //redirects to our login page

        die(); //stops the page from going any further

  }

  else

  {

        setcookie("user", $user, time()+3600);//sets our user cookie

                setcookie("pass", $pass, time()+3600);//sets our pass cookie

                header("Location: Indeks.html");//protected page

  }

 

?>

 

</body>

 

</html>

Link to comment
https://forums.phpfreaks.com/topic/205094-login-script-help/
Share on other sites

I strongly suggest using the code tags provided on the forums. Here's your problem:

 

1.) Need to check whether form is submitted before doing PHP

2.) Need to fix conditionals for checking if there are any rows

3.) Need to sanitize any values being used in your query

4.) If you want to use the header() function, you have to call it before you have any output on the page

 

 

Quick 3-minute rewrite

<?php

if(isset($_POST['user']) && isset($_POST['pass'])){

  include("connect.php"); //connects to our mysql database

  $user = $_POST['user']; //pulls the username from the form
  $pw = $_POST['pass']; //pulls the pass from the form
  $pass = md5($pw); //makes our password an md5

//sanitize inputs
  $user = mysql_real_escape_string($user);
  $pass = mysql_real_escape_string($pass);

  $login = mysql_query("SELECT * FROM `users` WHERE `username` = '$user' AND `password` = '$pass' "); //selects info from our table if the row has the same user and pass that our form does
  if(mysql_num_rows($login)==0) //if the username and pass are wrong
  {
        header("Location: Login.php");  //redirects to our login page
//DO NOT NEED        die(); //stops the page from going any further
  }
  else
  {
        setcookie("user", $user, time()+3600);//sets our user cookie
                setcookie("pass", $pass, time()+3600);//sets our pass cookie
                header("Location: Indeks.html");//protected page
  }
} else {

?>

<html>
<head>
<title>FreshCut </title>
<link rel="stylesheet" type="text/css" href="Styling.css" />
</head>
<body>
<h3 class="Innlogging">Login</h3>

<form action="Login.php" method="post" name="loginform" id="loginform">
  <p>Username:
  <input type="text" name="user">
  </p>
  <p>Password:
  <input type="password" name="pass">
  </p>
  <p>
  <input type="submit" name="Submit" value="Logg inn">
  </p>
  </form>

</body>

</html>
<?php
}
?>

 

Hi, I'm new to PHP and I'm trying to create a login script, but it doesn't seem to be working.

So if anyone can help me by telling what's wrong or missing I would greatly appreciate it.

 

Here is my code and the problem is that when i type the username and password it just remains on the same page and doesn't move to another. I have the username and password in the database and I'm connecting to it via include connection.php:

 

<html>

<head>

<title>FreshCut </title>

<link rel="stylesheet" type="text/css" href="Styling.css" />

</head>

<body>

<h3 class="Innlogging">Login</h3>

 

<form action="Login.php" method="post" name="loginform" id="loginform">

  <p>Username:

  <input type="text" name="user">

  </p>

  <p>Password:

  <input type="password" name="pass">

  </p>

  <p>

  <input type="submit" name="Submit" value="Logg inn">

  </p>

  </form>

 

<?php

 

  $user = $_POST['user']; //pulls the username from the form

  $pw = $_POST['pass']; //pulls the pass from the form

  $pass = md5($pw); //makes our password an md5

  include("connect.php"); //connects to our mysql database

  $login = mysql_query("SELECT * FROM users WHERE username = '$user' AND password = '$pass`"); //selects info from our table if the row has the same user and pass that our form does

  if(!mysql_num_rows($login)) //if the username and pass are wrong

  {

        header("Location: Login.php");  //redirects to our login page

        die(); //stops the page from going any further

  }

  else

  {

        setcookie("user", $user, time()+3600);//sets our user cookie

                setcookie("pass", $pass, time()+3600);//sets our pass cookie

                header("Location: Indeks.html");//protected page

  }

 

?>

 

</body>

 

</html>

Link to comment
https://forums.phpfreaks.com/topic/205094-login-script-help/#findComment-1073599
Share on other sites

Well, can you be more specific about what's not working?

 

IE: add the following line at the top of the script to see if there are any errors:

 

<?php
error_reporting(E_ALL);
?>

 

Add the following line for your query

 

<?php
  $login = mysql_query("SELECT * FROM `users` WHERE `username` = '$user' AND `password` = '$pass' ") or die('There was an error with the query!'.mysql_error()); //selects info from our table if the row has the same user and pass that our form does
?>

Link to comment
https://forums.phpfreaks.com/topic/205094-login-script-help/#findComment-1073686
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.