Jump to content

Recommended Posts

Hi everyone. I am in need of a login script, I found this one on the web. I am not familiar with cookies at all, the model doesn't seem to work. What is wrong?

 

At the moment, if you try to access protected_page.php it redirects to login.php which is what it should do. However, when I log in with the correct username and password it just reloads login.php and doesn't redirect to protected_page.php as it should.

 

Thanks is advance

 

login.php

<?php
$act = $_GET['act']; //retrives the page action
if(empty($act)) //if there is no action
{
  echo('<form action="login.php?act=auth" 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="Login">
  </p>
  </form>');
}
elseif($act == "auth") //if our page action = auth
{
  $user = $_POST['user']; //pulls the username from the form
  $pw = $_POST['pass']; //pulls the pass from the form<strong></strong>
  $pass = md5($pw); //makes our password an md5
  include("connect.php"); //connects to our mysql database
  $login = mysql_query("SELECT * FROM `users` WHERE `user` = '$user' AND `pass` = '$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: protected_pag.php");//instead of yourpage.php it would be your protected page
  } 
}
?>

 

protect.php

<?php
$user = $_COOKIE['user']; //gets the user from the cookies
$pass = $_COOKIE['pass']; //gets the pass from cookies
include("connect.php"); // connects to our database
$login = mysql_query("SELECT * FROM `users` WHERE `user` = '$user' AND `pass` = '$pass`"); //selects info from our table if the row has the same user and pass that our cookies do
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
}
?>

 

protected_page.php

<?php require("protect.php"); ?>

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

This line

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

in both login.php and protect.php is slightly wrong

 

change to

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

(note the last ' instead of `)

Link to comment
https://forums.phpfreaks.com/topic/162356-login-system-help/#findComment-858529
Share on other sites

This line

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

in both login.php and protect.php is slightly wrong

 

change to

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

(note the last ' instead of `)

I'm not sure about this code, but my script is working in this type of code

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

Just remove the single quote in every entity name

Link to comment
https://forums.phpfreaks.com/topic/162356-login-system-help/#findComment-860297
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.