Jump to content

Php Website Logging In Code


Nebin

Recommended Posts

Hi Guys. Really Stuck here. I am making a website, its basicall an online shop of sorts. I am making a login page for our customers and an admin page for us lot to upload new products to sql etc. Whats its meant to do is accept the username and pw then allow me to access the adminpage. Although its just saying that user doesnt exist all the time. I dont know why becuase the details are correct. :-\

 

admin_login page is the code below.

 

<?php

session_start();

if(isset($_SESSION["manager"])){

header("location:index.php");

exit();

}

 

?>

<?php

if(isset($_POST["username"])&&isset($_POST["password"])){

 

$manager = preg_replace('#[A-Za-z0-9]#i','',$_POST["username"]);

$password = preg_replace('#[^A-Za-z0-9]#i','',$_POST["password"]);

 

include"../storescripts/connect_to_mysql.php";

$sql = mysql_query("SELECT id FROM admin WHERE username='$manager' AND password='$password' LIMIT 1");

 

$existCount = mysql_num_rows($sql);

if($existCount == 1){

while($row = mysql_fetch_array($sql)){

$id = $row["id"];

}

$_SESSION["id"] = $id;

$_SESSION["manager"] = $manager;

$_SESSION["password"] = $password;

header("location: index.php");

exit();

}else{

echo 'That Information Is Incorrect. Try again <a href="index.php">Click Here</a>';

exit();

}

}

 

 

?>

 

 

and now the index.php which is what the admin see when they log in successfully.

 

<?php

session_start();

if(isset($_SESSION["manager"])){

header("location: admin_login.php");

exit();

}

//Be Sure To Check That This Manager Session Value Is Infact In The DataBase

$managerID = preg_replace('#[^0-9#i','',$_SESSION["id"]);

$manager = preg_replace('#[^A-Za-z0-9]#i','',$_SESSION["manager"]);

$password = preg_replace('#[^A-Za-z0-9]#i','',$_SESSION["password"]);

 

include "../strorescripts/connect_to_mysql.php";

$sql = mysql_query("SELECT * FROM admin WHERE id='$managerID' AND username='$manager' AND password='$password' LIMIT 1");

 

$existCount = mysql_num_rows($sql);

if($existCount == 0){

header("location:../index.php");

exit();

}

 

?>

Link to comment
Share on other sites

1. are you connecting to your db

 

2. have you tried echoing the number of rows to make sure you are grabbing any.

 

3. have you tried to debug your query yet. eg

$sql = mysql_query("SELECT id FROM admin WHERE username='$manager' AND password='$password' LIMIT 1") or die(mysql_error());

 

Link to comment
Share on other sites

  • 2 years later...

Noooo don't use any Adam Khoury (whatever) scripts (you're using one now). He stores passwords in his tutorials unhashed and unsalted and just tells you to go learn how. He's never heard of XSS, CSRF, and 2nd Order SQL Injection either. I would suggest either learning how to secure it first, build one from scratch, or follow another tutorial.

Link to comment
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.