Php会话没有启动

时间:2015-12-28 07:50:08

标签: php html session

我正在使用localhost并尝试创建一个登录系统,但我的会话没有启动,这是我的PHP代码

if(isset($_POST['_submit'])){
            session_start();
            if(empty($_POST['_email']) || empty($_POST['_password'])){
                echo "<script>window.alert('Invalid User EMAIL or PASSWORD!')</script>";
            }
            else{

                $email = $_POST['_email'];
                $password = $_POST['_password'];

                $select_user = "SELECT email,password FROM signup WHERE email='$email' AND password='$password'";
                $query = mysqli_query($conn,$select_user);
                $rows = mysqli_num_rows($query);

                if($rows>0){
                    $_SESSION['user_email'] = $email;
                    header('Location: ../index.php');
                }
                else{
                    echo "<script>window.alert('Invalid User EMAIL or PASSWORD!')</script>";
                }

            }
        }

在会话开始之后我想更改html代码,这是我的代码

<?php if(!empty($_SESSION['user_email'])){ ?>
<li><a href="#">Logout</a></li>
<?php } else { ?>
<li><a href="login/index.php">Login</a></li>
<li><a href="signUp/index.php">Sign Up</a></li>
<?php } ?>

这是我登录系统的html代码

<div class="outside">
            <form class="form-horizontal" role="form" method="post">
                <div class="form-group">
                    <label class="control-label col-sm-3 glyphicon glyphicon-user" for="email"></label>
                    <div class="control-label col-sm-8">
                        <input type="_email" name="_email" class="form-control" id="email" placeholder="Enter Email">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-3 glyphicon glyphicon-lock" for="password"></label>
                    <div class="control-label col-sm-8">
                        <input type="password" name="_password" class="form-control" id="password" placeholder="Enter Password">
                    </div>
                </div>
                <div class="form-group"> 
                    <div class="col-sm-offset-3 col-sm-3">
                        <button name="_submit" type="submit" class="btn btn-default" >Login</button>
                    </div>
                </div>
            </form>
            <p>Not a User? <a href="../signUp/index.php">Sign Up</a></p>
        </div>

谢谢,

3 个答案:

答案 0 :(得分:2)

您还需要启动需要使用会话

的会话
<?php
session_start();
if(isset($_SESSION['somekey']))
{
      //code here
}

答案 1 :(得分:1)

var slide4 = (function() { function animate() { var timelineFour = new TimelineLite(); timelineFour.to("#brand-welcome-para", 1, { 'margin-top': 0, 'opacity': 1, 'delay': 1 }) .to("#slide-1-creator-icon", 0.6, { 'top': 300, 'left': 45, 'width': 155, 'height': 155, ease: Elastic.easeOut }) .to("#avatar-one", 0.6, { 'left': 27, rotation: '-360' }) .to("#avatar-two", 0.6, { 'left': 100, rotation: '-360' }) .to("#avatar-four", 0.6, { 'left': '-50', rotation: '360' }) .to("#cover-one", 1, { width: 0, ease: Linear.easeNone }, 'firstLoop') .to("#slide-1-tandem-icon", 0.6, { 'top': 300, 'width': 155, 'height': 155, ease: Elastic.easeOut }) .to("#cover-one-two", 1, { width: 0, ease: Linear.easeNone }) .to("#slide-1-brand-icon", 0.6, { 'top': 300, 'right': 45, 'width': 155, 'height': 155, ease: Elastic.easeOut }) .to("#cover-two", 2, { width: 0, ease: Linear.easeNone }); var timelineChild = new TimelineMax({ repeat: -1, onComplete: complete, delay: 4 }); timelineChild.to("#rotator", 1, { rotation: -90, delay: 1 }, 'firstLoop') .to("#avatar-one", 1, { rotation: -270, delay: 1 }, 'firstLoop') .to("#avatar-two", 1, { rotation: -270, delay: 1 }, 'firstLoop') .to("#avatar-three", 1, { rotation: -270, delay: 1 }, 'firstLoop') .to("#avatar-four", 1, { rotation: -270, delay: 1 }, 'firstLoop') .to("#rotator", 1, { rotation: -180, delay: 1 }, 'secondLoop') .to("#avatar-one", 1, { rotation: -180, delay: 1 }, 'secondLoop') .to("#avatar-two", 1, { rotation: -180, delay: 1 }, 'secondLoop') .to("#avatar-three", 1, { rotation: -180, delay: 1 }, 'secondLoop') .to("#avatar-four", 1, { rotation: -180, delay: 1 }, 'secondLoop') .to("#rotator", 1, { rotation: -270, delay: 1 }, 'thirdLoop') .to("#avatar-one", 1, { rotation: -90, delay: 1 }, 'thirdLoop') .to("#avatar-two", 1, { rotation: -90, delay: 1 }, 'thirdLoop') .to("#avatar-three", 1, { rotation: -90, delay: 1 }, 'thirdLoop') .to("#avatar-four", 1, { rotation: -90, delay: 1 }, 'thirdLoop') .to("#rotator", 1, { rotation: -360, delay: 1 }, 'fourthLoop') .to("#avatar-one", 1, { rotation: 0, delay: 1 }, 'fourthLoop') .to("#avatar-two", 1, { rotation: 0, delay: 1 }, 'fourthLoop') .to("#avatar-three", 1, { rotation: 0, delay: 1 }, 'fourthLoop') .to("#avatar-four", 1, { rotation: 0, delay: 1 }, 'fourthLoop'); function complete(timelineChild) { timelineChild.restart(); // 0 sets the playhead at the end of the animation } } function restart() { timelineFour.restart(); } function getTimeline() { return timelineFour; } return { animate: animate, restart: restart, timeline: getTimeline } })(); 标记之前声明session_start()。看看它是否有效。

答案 2 :(得分:-1)

session_start()需要在if语句之外,并且通常在任何PHP代码执行之前运行。

session_start();
if(isset($_POST['_submit'])){

将session_start()放在要检查任何会话数据的所有PHP文件的顶部。如果您有一个集中的配置文件,只需将其放在那里。