Jump to content

[SOLVED] Login script problem


mjgdunne

Recommended Posts

Hi i have written a php login script, i have two levels of users level 1/2 saved as level in database. I need the scripts to be able to check to see if they are a member and if so what level they are, if level one go to screen A and if level 2 then go to screen B.

Any help would be greatly appreciated.

Thanks.

<?php

session_start();

ini_set( 'display_errors', '1' );
error_reporting ( 2047 );


$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "root", "root")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from signup form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];



$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result = mysql_query($sql);
if($result === false){
    exit('db error: ' . mysql_error());
}


// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// assign $_SESSION variables: $myusername, $mypassword and redirect to file "login_success.php"

    $userdata = mysql_fetch_row($result); //get the user row
    $_SESSION["myusername"] = $myusername;
    $_SESSION["mypassword"] = $mypassword;
$_SESSION["mylevel"]    = $mylevel;

    $debug = true; // Turn on DEBUGGING
    if($debug){
        //for debug only
        echo 'myusername: ' . $_SESSION["myusername"] . "<br />\n";
        echo 'mypassword: ' . $_SESSION["mypassword"] . "<br />\n";
        echo 'mylevel : '   . $_SESSION["mylevel"] . "<br />\n";
        exit();
    }

    header("location:login_success.php");

}

else {
    header("location:login_failure.php");
    // echo "Wrong Username or Password";
}
exit();

?>

Link to comment
https://forums.phpfreaks.com/topic/101700-solved-login-script-problem/
Share on other sites

It should be pretty easy:

 

<?php
if($count == 1){
   $valuesLogin = mysql_fetch_array($result);
   $mylevel = $valuesLogin['level'];
   $_SESSION['myusername'] = $myusername;
   $_SESSION['mypassword'] = $mypassword;
   $_SESSION['mylevel'] = $mylevel;
   if($mylevel == 1){
      header('Location: screenA.php');
   } else{
      header('Location: screenB.php');
   }
}
?>

 

Guess thats the way u wanted it.

<?php
if($count == 1){
   $valuesLogin = mysql_fetch_array($result);
   $mylevel = $valuesLogin['level'];
   $_SESSION['myusername'] = $myusername;
   $_SESSION['mypassword'] = $mypassword;
   $_SESSION['mylevel'] = $mylevel;
   if($mylevel == 1){
      header('Location: screenA.php');
   } else{
      header('Location: screenB.php');
   }
} else{
   header('Location: login_failure.php');
}
?>

 

Its the same way u used it, i just added the if/else for $mylevel.

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.