dsvbtgo639708 2015-11-02 17:44 采纳率: 0%
浏览 53
已采纳

在帐户页面上获取自动增量值(登录系统)

So I'm creating a login section. However, I'm not sure if it actually logs the user in. I have a table with UserID (A_I), email, password and all the usual stuff. So when I log in, I'm trying to fetch and echo the UserID on the account page, just to show that the user is logged in, but still haven't got any success with it.

Login form:

<form method="post" action="">
            <div class="FormElement">
                    <input  name="email" type="text" required="required" class="TField"  placeholder="Email">
            </div>
            <div class="FormElement">
                    <input  type="Password" required="required" class="TField"  placeholder="Password">
            </div>
            <div class="FormElement">
                    <input name="LogIn"  type="submit" class="button" value="LogIn">
            </div>
        </form>

Login php script:

    <?php require 'Connections/Connections.php'; ?>
<?php

    if (isset($_POST['LogIn'])){

            $EM = $_POST['email'];
            $PW = $_POST['password'];
            $result = $con->query("select * from user where Email='$EM' AND Password='$PW'");

            $row = $result->fetch_array(MYSQLI_BOTH);
            session_start();
            $_SESSION["UserID"] = $row['UserID'];
            header('Location: Account.php');
    }
 ?>

As you can see in the script, we're heading to Account.php, where I am basically trying to:

<?php echo $_SESSION["UserID"]; ?>

However, no UserID is being echo'ed. No errors, notices or warnings.

The Connections.php ($con) is just a normal connection to the MYSQLI table, and it connects just fine.

Pretty new around here and any help is appreciated!

Thanks in advance.

  • 写回答

4条回答 默认 最新

  • dsnpjz6907 2015-11-02 17:54
    关注

    You have to add the atribute name for your password field. Also it's good to use prepared statements and PDO. Finally, when you redirect your page add the exit method Why I have to call 'exit' after redirection through header('Location..') in PHP?.

    <form method="post" action="youraction.php">
        <div class="FormElement">
            <input  name="email" type="text" required="required" class="TField"  placeholder="Email">
        </div>
        <div class="FormElement">
            <input name="password" type="Password" required="required" class="TField"  placeholder="Password">
        </div>
        <div class="FormElement">
            <input name="LogIn"  type="submit" class="button" value="LogIn">
        </div>
    </form>
    
    <?php
    
    if (isset($_POST['LogIn'])) {
        session_start();
    
        try {
            //Make your connection handler to your database
            $conn = new PDO("mysql:host=".$servername.";dbname=".$database, $username, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
    
            $sql = "SELECT * FROM user WHERE Email = :email AND Password = :password";
            $stmt = $conn->prepare($sql);
            //Execute the query
            $stmt->execute(array(':email'=>$email, ':password'=>$password));
            $row = $stmt->fetch();
            if (count($row) > 0) {
                $_SESSION["UserID"] = $row['UserID'];
                header('Location: Account.php');
                exit();
            }
    
        } catch(PDOException $e) {
            echo $e->getMessage();
            die();
        }
    
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面