发布/重定向/获取适用于XAMPP,返回生产服务器上的空白页

时间:2017-08-22 21:20:20

标签: php session post

在我过去几周一直在做的项目的终点线附近,我最近第一次将它上传到我的服务器以检查是否有任何问题。虽然几乎所有东西都完美无瑕,但有一个问题我无法解决这个问题!

说明

这是一个在线商店,用户的购物车存储在$_SESSION['id']变量中。一切都是通过POST请求处理的。单击<input type="submit" name="Submit" value="Add to cart" id="addToCart">按钮后,表单会检查$_SESSION['id']中是否已存在产品ID。如果是这种情况,则会调用break以防止多次添加产品。由于表单的操作指向同一个文件,理论上它应该刷新。为了规避用户在POST请求之后刷新页面/使用后退按钮进行令人讨厌的通知(并且为了防止多次发送表单),我得到了一个帖子/重定向/获取代码片段{{3发帖。正如(我相信)我在one.com上的共享主机运行Apache,我使用了重写方法。

XAMPP上的情况:

访客导航到/sub/0.php(第一个产品的网站),选择他的衣服尺寸并点击添加到购物车。之后,产品ID会附加到$ _SESSION ['id'],页面会在最后刷新?status=success,并在右上角显示一个小购物车。

生产服务器上的情况(已启用错误消息):

访客导航到/sub/0.php(第一个产品的网站),选择他的衣服尺寸并点击添加到购物车。产品ID被正确附加(我可以检查cart.php并显示),虽然刷新时有EITHER

  1. 空白页
  2. 一个显示“0”的空白页面(这似乎是我添加到购物车的第一个产品的ID,我先添加了ID为3的产品,然后显示了3)
  3. 警告“已经在购物车中”。这是奇怪的部分!警告位于if(isset($ _ SESSION ['id']))语句中的代码中,因此只有在按下按钮后才能看到它。 “已经在购物车中”甚至会在页面重新加载时显示。我必须手动点击URL栏并按Enter键以获取视图。在这个Stackoverflow中,您可以详细了解我的意思。
  4. 我的代码出了什么问题,为什么它在XAMPP上运行但在生产服务器上不起作用? PGR似乎根本不起作用(或者代码永远不会因为调用中断而执行PGR),它也不会在URL后面附加status = success。

    我真的很茫然,每一个输入都会非常感激!

    以下是产品网站的所有代码:

    <?php
    session_start();
    $id=0;
    include (__DIR__."/../includes/php/database.php");
    
    if (isset($_POST['Sumbit'])) {
        // echo "submitted";
        if (isset($_SESSION['id'])) {
    
            if (gettype($_SESSION['id'])=="string") {       //if there are already multiple IDs stored withinSESSION['id'] --> String
                for ($i=0; $i < strlen($_SESSION['id']); $i++) { 
                    if ($id == $_SESSION['id'][$i]) {      //if there are multiple IDs already in cart and one of them matches with $id (--> multiple items are in the cart, but the current one is not)
                        echo "<script>alert('Already in Cart!')</script>";        //before implementing PRG, this alert appeared always when trying to add the product twice. Since I implemented PRG, it wasn't triggered once in XAMPP
                        $alreadyInCart=true;
                        break;
                    }
                }
                if (!isset($alreadyInCart)) {               //if already some IDs stored within$_SESSION['id'], but$id is not(--> more than 1 other products are in the cart, but the current one is not)
                    $_SESSION['id'].=$id;
                    $_SESSION['size'].=$_POST['Size'];
                }
            }else{                                          //if$_STRING['id'] isn't a 'string' (it's an 'integer' --> just one item in the cart), there's no need (nor possibility) to loop.
                echo $_SESSION['id'];                       
                if($id==$_SESSION['id']){                   //Just one item in cart, but that matches with$id (--> current item already in cart)
                    $alreadyInCart=true;
                    echo "<script>alert('Nur 1 Artikel im Warebkorb-Already in Cart!')</script>"; //before implementing PRG, this alert appeared always when trying to add the product twice. Since I implemented PRG, it wasn't triggered once in XAMPP
                }else{                                      //Just 1 item in cart but $id doesn't match with $_SESSION['id'](--> current item not yet in cart)
                    $_SESSION['id'].=$id;
                    $_SESSION['size'].=$_POST['Size'];
                }
            }
        }else{                                              //If $_SESSION['id'] doesn't even exist yet --> = instead of .=
            $_SESSION['id']=$id;
            $_SESSION['size']=$_POST['Size'];
        }
        // Post redirect get https://en.wikipedia.org/wiki/Post/Redirect/Get
        $get_info = "?status=success";
        header("Location: ".$_SERVER['REQUEST_URI'].$get_info);
        echo $_GET['status'];
        exit();
    }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta name="theme-color" content="#000000" />
        <title>CNTRST</title>
        <link rel="stylesheet" type="text/css" href="../includes/css/styles1.css">
        <link rel="stylesheet" type="text/css" href="../includes/fonts/fonts.css">
        <script type="text/javascript" src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
        <script type="text/javascript" src="../includes/js/main.js"></script>
    
        <link id="essential" rel="stylesheet" type="text/css" href="../includes/css/sub_styles.css">
        <?php 
            if (isset($_SESSION['id'])) {
                if (isset($_GET['status']) && $_GET['status']=="success") {
                    echo '<script type="text/javascript" src="../includes/js/cart.js"></script>';
                }
            }
         ?>
    
    </head>
        <body>
            <div class="content">
                <?php 
                    if (isset($_SESSION['id'])) {
                            include(__DIR__)."/../includes/php/cart_icon.php";
                    }
                 ?>
                <img id="logo" src="../includes/media/img/icons/logo.svg"></img>
                <div class="slideshow">
                    <img src="../includes/media/img/slideshow/bg.png" id="slideshow_bg">
                    <img class="slideshow_objects" src="../<?php echo $product->product_image; ?>">
                </div>
    
                <div class="description">
                    <h1><?php echo $product->product_name; ?></h1>
                    <h2></h2>
                    <ul>
                        <?php echo $product->product_desc; ?>
                    </ul>
                    <strong id="price"><?php echo $product->product_price; ?> <span class="currency">€</span></strong>
                    <form action="<?php echo $id ?>.php" method="post">
                        <fieldset>
                            <label for="Size">Size</label>
                            <select name="Size" id="Size">
                                <option value="0">XS </option>
                                <option value="1">S </option>
                                <option value="2">M </option>
                                <option value="3">L </option>
                                <option value="4">XL </option>
                                <option value="5">XXL </option>
                            </select>
                            <input type="hidden" name="Product_Name" value="Marble print"><br>
                            <input type="submit" name="Sumbit" value="Add to cart" id="addToCart">
                        </fieldset>
                    </form>
                </div>
            </div>
                <?php include "../includes/php/footer.php" ?>
            </div>
        </body>
    </html>
    

    修改

    在代码顶部添加error_reporting(E_ALL); ini_set('display_errors', '1');后,我没有收到空白页,但显示错误消息:

      

    警告:无法修改标头信息 - 已在/ customers / 6中发送的标头(在/customers/6/5/2/cntrst.at/httpd.www/includes/php/database.php:1处开始输出)第40行/5/2/cntrst.at/httpd.www/sub/0.php注意:未定义的索引:/comersomers/6/5/2/cntrst.at/httpd.www/sub/0.php中的状态在第41行

    database.php包含:

       <?php
         ini_set('session.cache_limiter','public');
        session_cache_limiter(false);
        $host = 'localhost';
        $db   = 'DB_NAME';
        $user = 'root';
        $pass = '';
        $charset = 'utf8';
    
        $dsn = "mysql:host=$host;dbname=$db;charset=$charset";
        $opt = [
            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES   => false,
        ];
        $con = new PDO($dsn, $user, $pass, $opt);
        if (isset($cart)) {
    
        }else{
            $product = $con->query("
                        SELECT * FROM products
                        WHERE id=$id
            ");
            $product = $product->fetch(PDO::FETCH_OBJ);
        }
    
        ?>
    

0 个答案:

没有答案