简体   繁体   English

PHP:单击表单提交按钮时会话变量丢失?

[英]PHP: session variable lost on clicking form submit button?

When I click form submit button session variable lost and it shows message that session is not set.当我单击表单提交按钮会话变量丢失时,它显示会话未设置的消息。 I have another confusion that it has only problem when I set session of login variable or those variables which are set in other page of my site.我有另一个困惑,它只有在我设置登录变量的会话或在我网站的其他页面中设置的那些变量时才有问题。
When I set some random session variable on the top of this page it works fine and session variable does not lose anymore.当我在此页面顶部设置一些随机会话变量时,它工作正常并且会话变量不再丢失。 I have also checked some other related links in stack overflow but nothing found solution我还检查了堆栈溢出中的其他一些相关链接,但没有找到解决方案

Here is the code of addProduct.php page这是 addProduct.php 页面的代码

        <?php

    //var_dump($_SESSION);
    if(!(isset($_SESSION['login']))) {

        echo "session is not set";
    }
    else {
        //header("location:index.php");
        echo "session is set";
        //$user_email=$_SESSION['user_email'];



    ?>
        <html>
        <body>

        <form action="addproduct.php" method="post">

            <input type="text" name="name" value="">
            <input type="submit" name="submit" value="submit">

        </form>


        </body>
        </html>
        <?php
        if(isset($_POST['submit']))
        {
            $name = $_POST['name'];
           // $_SESSION['user_email']=$_SESSION['user_email'];
            echo $name;
        }
        ?>
    <?php }
    ?>

Code of index.php (header file) page from where I get into this page我进入此页面的 index.php(头文件)页面的代码

<?php
    session_start();
    include("db.php");
    ?>

    <html xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html">
    <head>
        <title>Home</title>
    </head>
    <body>

    <a href="index.php">Home</a></br></br>

    <?php if(isset($_SESSION['login']) ) {
    if($_SESSION['user_status']=="admin")
    {

    ?>
    <a href="index.php?page=addproduct">Post an Ad</a></br></br>
    <?php }
    }

    ?>

    <?php if(isset($_SESSION['user_fname']) && isset($_SESSION['user_lname']) && isset($_SESSION['user_email']))
    {
    ?>
    <a href="index.php"><?php echo $_SESSION['user_fname'] . " " . $_SESSION['user_lname'] . " " . $_SESSION['user_status']; ?></a></br></br>
    <?php
    }
    else
    {
    ?>
    <a href="login.php">Login</a></br></br>
    <a href="UserSignup.php">SignIn</a></br></br>
    <?php }
    if(isset($_SESSION['user_fname']) && isset($_SESSION['user_lname']) && isset($_SESSION['user_email']))
    {
    ?>
    <a href="logout.php">Logout</a></br></br>
    <?php }
    ?>
    <div id="content">
        <?php


        if(isset($_GET['page']))
        {
            $p=$_GET['page'];

            $page =  $p . ".php";
            //echo $page;
            if(file_exists($page))
            {include($page);
            }
            elseif($page=="")
                echo "this is home page";
            else
            {echo "Not found";}
        }

        else
        {
            include ("showAds.php");
        }
        ?>

    </div>










    </body>
    </html>

Code of login.php login.php的代码

    <?php
    session_start();
    if(isset($_SESSION['user_fname']) && isset($_SESSION['user_lname']) && isset($_SESSION['user_email'])) {
        header("location:index.php");
        exit();
    }
        else
        {

    ?>

    <html>

    <head><title>Login</title></head>
    <body>
    <form action="login.php" method="post">
        <input type="email" name="user_email" placeholder="USER EMAIL" REQUIRED>
        <input type="password" name="user_password" placeholder="USER PASSWORD" REQUIRED>

        <input type="submit" name="Go" value="SUBMIT!" placeholder="USER NAME" REQUIRED>
        </br></br><a href="UserSignup.php">SignIn with new account</a></br>



    </form>

    <?php
    include("db.php");
    /*if(isset($_POST['Go'])) {    SIGNUP
        $user_name = $_POST['user_name'];
        $user_password = $_POST['user_password'];
        $user_email = $_POST['user_email'];
        echo $user_name . "<br>";
        echo $user_email . "<br>";
        echo $user_password . "<br>";
        $sql = "INSERT INTO user(user_name,user_email,user_password) VALUE ('$user_name','$user_email','$user_password')";
        if(mysqli_query($conn,$sql))
        {
            echo "stored";
            header("location:http://localhost/window.php");
        }
        else
        {
          die(mysqli_error($sql));
        }
    }*/
    if(isset($_POST['Go']))
    {
        $user_email = $_POST['user_email'];//real_escape_string
        $user_password = $_POST['user_password'];
        $login_query="SELECT * FROM user WHERE user_email='$user_email' AND user_password='$user_password'";
        $run=mysqli_query($conn,$login_query);
        if(mysqli_num_rows($run)>0)

        {
            $res = mysqli_query($conn, "SELECT * FROM ecommerce.user WHERE user_email='$user_email'");

            while ($record = mysqli_fetch_array($res)) {

                $_SESSION['user_fname']=$record['user_fname'];
                $_SESSION['user_lname'] = $record['user_lname'];
                $_SESSION['user_status'] = $record['user_status'];
                $_SESSION['user_id'] = $record['user_id'];
                $_SESSION['user_password'] = $record['user_password'];

            }


            $_SESSION['user_email']=$user_email;
            $_SESSION['login']="true";

           //echo $_SESSION['user_fname'] . $_SESSION['user_lname'];


            header("location:index.php");
            exit();
        }
        else
            echo "<p style='color: red; margin-top: -28px;'>User name or password incorrect</p>";
    }




    ?>

    </body>
    </html>
    <?php }?>

This error you showed in your other question which was marked as an exact duplicate of this one:您在另一个问题中显示的此错误被标记为与此问题完全相同:

Notice: A session had already been started - ignoring session_start() in C:\xampp\htdocs\ecommerce\showAds.php on line 2注意:会话已经开始 - 忽略第 2 行 C:\xampp\htdocs\ecommerce\showAds.php 中的 session_start()

Your showAds.php page which you didn't include, (most likely) contains session_start();您未包含的showAds.php页面(很可能)包含session_start(); and it should be removed from inside that file.并且应该从该文件中删除它。

index.php has an include and session_start(); index.php有一个 include 和session_start();

else
    {
        include ("showAds.php");
    }

So one of your if statements failed.所以你的一个if语句失败了。

That is why you're getting that error.这就是您收到该错误的原因。

All pages using sessions require that session_start();所有使用会话的页面都需要session_start(); be included and should be the first line of your script, which isn't shown in addProduct.php .被包括在内,并且应该是脚本的第一行,它没有显示在addProduct.php中。

Also make sure you're not outputting before header.还要确保你没有在标题之前输出。 If you are, consult the following on how to fix it:如果是,请参阅以下有关如何修复它的信息:

Add error reporting to the top of your file(s) which will help find errors.在文件顶部添加 错误报告,这将有助于发现错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.旁注:显示错误应该只在暂存中完成,而不是在生产中。

You have to add session_start();您必须添加session_start(); in your addProduct.php to be able to access session contents!在您的 addProduct.php 中,以便能够访问会话内容!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM