简体   繁体   中英

Login system PHP won't return anything

I need help to fix my database. I don't understand why it doesn't returns anything. Using USBWebserver with MySql. The login file doesn't returns anything in the browser not even an error message. Please help me

<!DOCTYPE html>

<?php
mysql_connect('localhost', 'root', 'usbw');
mysql_select_db('login');

if(isset($_POST['login'])) {
  $gebruiker = mysql_real_escape_string($_POST['naam']);
  $wachtwoord = sha1(mysql_real_escape_string($_POST['password']));

  $query = mysql_query("SELECT user_id, username, userlevel
    FROM users
    WHERE username = '$gebruiker'
    AND password = '$wachtwoord' ");

  // aantal rijen uit de database halen
  $result = mysql_num_rows($query);

  // session variabele starten waaraan je data koppelt
  $sess_var = mysql_fetch_assoc($query);
  $userlevel = $sess_var['userlevel'];

  if ($result == 1){

    // sessie starten en variabelen in de sessie opnemen
    session_start();
    $_SESSION['userlevel'] = $userlevel;
    $_SESSION['gebruiker'] = $gebruiker;
    $_SESSION['wachtwoord'] = $wachtwoord;

    // userlevel controleren en vervolgens bezoeker doorsturen
    // naar de juiste pagina's voor zijn/haar rechten
    if($userlevel == 1){
      header('location:index.php');
      exit();
    } elseif($userlevel == 3) {
      header('location:index.php');
      exit();
    } else {
      header('location:index.php');
      exit();
    }
  }
  mysql_error();
?>

<html>
  <head>
    <style type="text/css">
      ul {list-style: none;}
    </style>
  </head>
  <body>

    <form method="post" action="">
    <ul>
      <li>Gebruikersnaam:</li>
      <li><input typ="text" name="naam" /></li>
      <li>Wachtwoord:</li>
      <li><input type="password" name="password" /></li>
      <li><input type="submit" name="login" value="login" /></li>
    </ul>
  </form>
</body>

If your code sample above is complete, you're missing a closing parenthesis. if(isset($_POST['login'])) { is not closed.

Also, you are outputting content to the client before starting the session. session_start must be called before anything is written for output. Move everything before the opening <?php tag to after a point where session_start(); is called.

As others have mentioned, stop using mysql_* functions. Look into mysqli or pdo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM