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条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)