简体   繁体   中英

PHP Login System - Give username small, when logged in username like in Database

I've finished here a login system in php with MySQL, but I want now that when I write my username in the login.php it also can be in small letters, and when I'm logged in, that my username will be changed to the original username, how it's registered in the database.

<?php

  session_start();

  if(isset($_SESSION["username"]))

    header("location:h_inc/h_pages/me.php");

    if(isset($_POST["submit"])){

      if(empty($_POST["username"]) || empty($_POST["password"])){
        $empty_fields = array();
        $empty_fields['error'] = "<font color=\"red\">Please fill all fields.</font>";

      }else{
        if(mysql_num_rows(mysql_query("SELECT * FROM users WHERE username = '".mysql_real_escape_string($_POST["username"])."' AND password = '".mysql_real_escape_string(md5($_POST["password"]))."' AND rank = '9'")) > 0){
          $empty_fields['true'] = "<font color=\"green\">You are now logged in.</font>";

          $_SESSION["username"] = $_POST["username"];
          $_SESSION["password"] = md5($_POST["password"]);

          echo "<script type='text/javascript'> window.location.href = 'index.php';</script>";
        }else{
          $empty_fields['error'] = "<font color=\"red\">Your datas are incorrect.</font>";
        }
      }
    }

?>

Instead of mysql_num_rows fetch the result from your query and use the value therein to display the username.

As @Naruto pointed out in the comments: Think about switching to PDO.

Return your query values ,then get your original name from return value and assign it in session variable

$result = mysql_query("SELECT * FROM users WHERE username = '".mysql_real_escape_string($_POST["username"])."' AND password = '".mysql_real_escape_string(md5($_POST["password"]))."' AND rank = '9'");
    if($result) {
    while ($row = mysql_fetch_array($result)) {
        $username = $row["username"];
    }
     $_SESSION["username"] = $username;
     $_SESSION["password"] = md5($_POST["password"]);
     echo "<script type='text/javascript'> window.location.href = 'index.php';</script>";
    }else{
          $empty_fields['error'] = "<font color=\"red\">Your datas are incorrect.</font>";
        }

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