dongyong3554 2015-02-16 22:35
浏览 26
已采纳

Mysqli登录准备好的声明

Okay, I haved been maked a register system, but when I tried to make the login system it didn't work. When the user log in with the correct username and password its post "You are now logged in", but when I refresh the site, It says I need to login. And I has to ask how I could check the user is logged in exempel If the user logged in so echo this and if not so echo this..

Here is my code:

session_start();
            require 'inc/connect.php';

            if(isset($_POST['name'], $_POST['password'])){
                if(!empty($_POST['name'])){
                    if(!empty($_POST['password'])){


                        $username = $_POST['name'];
                        $password = $_POST['password'];
                        $user = $_SESSION['name'];

                        // KRYPTERING
                        $md5 = md5($password);
                        $sha1 = sha1($md5);
                        $crypt = crypt($sha1, 'st');
                        $md51 = md5($crypt);
                        $sha12 = sha1($md51);
                        $crypt1 = crypt($sha12, 'st');
                        $kode = base64_encode($crypt1);

                        $check = $mysqli->prepare("SELECT username, password FROM user WHERE username=? && password=?");
                        $check->bind_param("ss", $username, $kode);
                        $check->execute();
                        $check->bind_result($username, $kode);
                        $print = $check->fetch();
                        $check->close();

                        if ($print)
                            echo "You are now logged in!";
                        else
                            echo "Username / Password was incorrect!";

                    } else
                        echo "You need something!";
                } else
                    echo "You need something!";
            }
  • 写回答

1条回答 默认 最新

  • dsy48837 2015-02-16 23:08
    关注

    You need to put the username in the session on successful login, to check for it at the top of the page, and redirect to a different when its already set.

    session_start();
                require 'inc/connect.php';
    
    if(isset($_SESSION['username']))
    {
      //already logged in, redirect
      header('Location: otherpage.php');
      exit;
    }
    
                if(isset($_POST['name'], $_POST['password'])){
                    if(!empty($_POST['name'])){
                        if(!empty($_POST['password'])){
    
    
                            $username = $_POST['name'];
                            $password = $_POST['password'];
                            $user = $_SESSION['name'];
    
                            // KRYPTERING
                            $md5 = md5($password);
                            $sha1 = sha1($md5);
                            $crypt = crypt($sha1, 'st');
                            $md51 = md5($crypt);
                            $sha12 = sha1($md51);
                            $crypt1 = crypt($sha12, 'st');
                            $kode = base64_encode($crypt1);
    
                            $check = $mysqli->prepare("SELECT username, password FROM user WHERE username=? && password=?");
                            $check->bind_param("ss", $username, $kode);
                            $check->execute();
                            $check->bind_result($username, $kode);
                            $print = $check->fetch();
                            $check->close();
    
                            if ($print)
                            {
                                //redirect to different page for the message
                                //echo "You are now logged in!";
                                $_SESSION['username'] = $username;
                                 header('Location: otherpage.php');
                                 exit;
                            }
                            else
                                echo "Username / Password was incorrect!";
    
                        } else
                            echo "You need something!";
                    } else
                        echo "You need something!";
                }
    

    Then in the other pages, you'll want something like:

    session_start();
    if(!isset($_SESSION['username']))
    {
      //not logged in, redirect to login form
      header('Location: login_form.php');
      exit;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗