Jump to content

Help with php admin login..


Artur

Recommended Posts

Hallo I have a problem.

This is my code:

<?php include 'connect.php';
?>

<html>
<head>
<title>Admin Insert page!</title>
</head>
<body>
<?php
error_reporting(-1);ini_set('display_errors',1);
if (isset($_POST['submit'])){
$name = $_POST['name'];
$password = $_POST['password'];
$result = mysql_query("SELECT * FROM users WHERE user='$name' AND password='$password'");
$num = mysql_num_rows($result);
if($num == 0){
echo "Bad login, go <a href='login.php'>back</a>";
}else{
session_start();
$_SESSION['name'] = $name;
header("Location: admin.php");
}
}else{
?>
<form action='login.php' methody='post'>
Username: <input type='text' name='name'/><br />
Password: <input type='password' name='password'/><br />
<input type='submit' name='submit' value='Login' />

</body>
</html>

I try to use console to find the problem but I didn't....

I know that there is some problem with $num

Can somebody help me?

Thank you.

Edited by Artur
Link to comment
Share on other sites

You should not be using any mysql_* functions, instead use mysqli_* or PDO

 

Saving the raw password into a database is not good either, but at least escape any data inserting into mysql.

 

Try this

<?php
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors',1);
session_start();
include('connect.php');

if (isset($_POST['name']) && trim($_POST['name']) != '' && isset($_POST['password']) && trim($_POST['password']) != ''){
$name = trim($_POST['name']);
$password = trim($_POST['password']);
$escape_name = mysql_real_escape_string($name);
$escape_password = mysql_real_escape_string($password);
$result = mysql_query("SELECT * FROM users WHERE user='{$escape_name}' AND password='{$escape_password}'");

if($result){
$_SESSION['name'] = $name;
header("Location: admin.php");
exit();
}
}else{
?>
<html>
<head>
<title>Admin Insert page!</title>
</head>
<body>

<form action='login.php' methody='post'>
Username: <input type='text' name='name'/><br />
Password: <input type='password' name='password'/><br />
<input type='submit' name='submit' value='Login' />

</body>
</html>
<?php
}
?>
Edited by QuickOldCar
Link to comment
Share on other sites

Found the issue

<form action='login.php' methody='post'>

<?php
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors',1);
session_start();
include('connect.php');

if (isset($_POST['name']) && trim($_POST['name']) != '' && isset($_POST['password']) && trim($_POST['password']) != ''){
$name = trim($_POST['name']);
$password = trim($_POST['password']);
$escape_name = mysql_real_escape_string($name);
$escape_password = mysql_real_escape_string($password);
$result = mysql_query("SELECT * FROM users WHERE user='{$escape_name}' AND password='{$escape_password}'");

if($result){
$_SESSION['name'] = $name;
header("Location: admin.php");
exit();
}else{
header("Location: login.php");
exit();
}
}else{
?>
<html>
<head>
<title>Admin Insert page!</title>
</head>
<body>

<form action='login.php' method='post'>
Username: <input type='text' name='name'/><br />
Password: <input type='password' name='password'/><br />
<input type='submit' name='submit' value='Login' />

</body>
</html>
<?php
}
?>
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.