Question

This is my auth file i got a db with

username password email positief(here is a "1" or a "0", a "1" if you got admin rights)

I want my code to recognize the "1" and give you acces to a page if you have the 1"" if you dont you can't enter it.

<html>
<body>

<?php
session_start();    // Create the session, Ready for our login data.

$username = $_POST['username']; // Gets the username from the login.php page.
$password = $_POST['password']; // Gets the plain text password.
$database = "cmict_test";

// Connect to your database
mysql_connect("","","") or die(mysql_error());
mysql_select_db("$database");

$query = "SELECT * FROM users WHERE password = '$password' LIMIT 1"; 

$username = mysql_real_escape_string($username); // just to be sure.

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
    $resusername = $row['username']; // username from DB
    $respassword = $row['password']; // password from DB
    $resemail = $row['email']; // email from db
    $admin = $row['positief'];
}

// Are they a valid user?

if ($resusername == $username && $respassword == $password) {
    // Yes they are.
    // Lets put some data in our session vars and mark them as logged in.
    $_SESSION['loggedin'] = "1";
    $_SESSION['email'] = $resemail;
    $_SESSION['username'] = $resusername;

    header("location:navigra.php");
}else{
    // No, Lets mark them as invalid.
    $_SESSION['loggedin'] = "0";
    echo "Sorry, Invalid details.<br>";
    die ('klik <a href="testlogin.php">hier</a> om opnieuw te proberen.');
}

if ($admin == 1) {
$_SESSION['logadmin'] = "1";
} else {
$_SESSION['logadmin'] = "0"
echo "You are no admin";
die ('klik <a href = "index.html">hier</a>');
}


?>

</body>
</html>

and this is what i put on top of the page to check if you are loggedin and if you got admin rights

<?php
session_start(); // Start the session
$loggedin = $_SESSION['loggedin']; // Are they loggedin?
$logadmin = $_SESSION['logadmin']; // Are they admin?

// They are not logged in, Kill the page and ask them to login.
if ($loggedin != "1") {
die('Sorry you are not logged in, please click <a href="adminlogin.php">Here</a> to    
login');}

if ($logadmin != "1") {
die ('You have no POWER here');}

?>

Can someone help me with this? i would appreciate it alot.

Thank you in advance!

Greetings,

DTcodedude

Was it helpful?

Solution

<html>
<body>

<?php
session_start();    // Create the session, Ready for our login data.

$username = addslashes($_POST['username']); // Gets the username from the login.php page.
$password = addslashes($_POST['password']); // Gets the plain text password.
$database = "cmict_test";

// Connect to your database
mysql_connect("","","") or die(mysql_error());
mysql_select_db("$database");

$query = "SELECT * FROM users WHERE username = '$username' and password = '$password' LIMIT 1"; 

$username = mysql_real_escape_string($username); // just to be sure.

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
    $resusername = $row['username']; // username from DB
    $respassword = $row['password']; // password from DB
    $resemail = $row['email']; // email from db
    $admin = $row['positief'];
}

// Are they a valid user?

if ($resusername == $username && $respassword == $password) {
    // Yes they are.
    // Lets put some data in our session vars and mark them as logged in.
    $_SESSION['loggedin'] = "1";
    $_SESSION['email'] = $resemail;
    $_SESSION['username'] = $resusername;
    $_SESSION['logadmin'] = $admin;

    header("location:navigra.php");

}else{
    // No, Lets mark them as invalid.
    $_SESSION['loggedin'] = "0";
    echo "Sorry, Invalid details.<br>";
    die ('klik <a href="testlogin.php">hier</a> om opnieuw te proberen.');
}



?>

</body>
</html>

EDIT :

-added check on username in sql query (as suggested by Jason)

-added addslashes for basic protection against SQL injection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top