甜蜜警报不与$ _session合作

时间:2016-08-25 06:30:10

标签: javascript php mysql session sweetalert

如果sql为true,我想显示一个甜蜜的警报,但它不起作用。 这是后端部分。

<?php
include "../../../config/config.php";

if (isset($_POST['submit-slider']))
 {
    $title = $_POST['title-slider'];
    $description = $_POST['description-slider'];
    $street = $_POST['street-slider'];
    $phone = $_POST['phone-slider'];
    $trip = $_POST['trip-slider'];
    $web = $_POST['web-slider'];
    $email = $_POST['email-slider'];

    $target_dir = "../../../img/homepage/screen_4/";
    $target_file2 = "" . basename($_FILES["img-slider"]["name"]);
    $target_file = $target_dir . basename($_FILES["img-slider"]["name"]);


    $uploadOk = 1;

    $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);


    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        //  header("Location: /dashboard/views/slider/add_slider.php");
        // if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["img-slider"]["tmp_name"], $target_file)) {


            //echo "The file ". basename( $_FILES["img"]["name"]). " has been uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }


        $query = "INSERT INTO images(title,
                                  description,
                                  street_1,
                                  phone,
                                  url,
                                  trip_link,
                                  img,
                                  email
                                )

                                    VALUES ('" . $title . "', '"
                . $description . "', '"
                . $street . "', '"
                . $phone . "', '"
                . $web . "', '"
                . $trip . "', '"
                . $target_file2 . "', '"
                . $email . "')";

        $result = mysqli_query($con, $query);

        if ($result) {

            $_SESSION['succes_slider'] = 1;
            header("Location: /dashboard/views/slider/add_slider.php");
        } else {
//            $_SESSION['succes_slider'] = 0;
            header("Location: /dashboard/views/slider/add_slider.php");
        }
    }
}
?>

这是我要展示甜蜜警报的页面。

<?php
session_start();
if (!isset($_SESSION['name'])) {
    header("Location: /dashboard/login.php");
}


//include "../config/config.php";
include "../../components/header.php";
include "../../components/menu.php";


if (isset($_SESSION['succes_slider']) == 1) {
    echo"  <script>
            $(document).ready(function() {
                swal('Succes','','succes');
            });
            </script>";
}


?>

</aside>


<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            Hello
            <small>HI</small>
        </h1>
    </section>

    <!-- Main content -->
    <section class="content">

我不知道为什么不工作,我知道..我的代码在MySql注入时容易受到攻击,但我现在没有在这方面工作。

甜蜜警报正在我的登录页面上工作,如果可以帮助别人知道为什么不在其他页面上工作..

以下是登录的后端

if (isset($_POST['login'])) {
    $email = mysqli_real_escape_string($con, $_POST['email']);
    $password = mysqli_real_escape_string($con, $_POST['password']);



    $sql = "SELECT * FROM users WHERE email='" . $email . "' AND password='" . md5($password) . "'";


    $result = mysqli_query($con, $sql);


    $row_count = mysqli_num_rows($result);


    if ($row_count != 0) {
        $row = mysqli_fetch_assoc($result);


        $_SESSION['email'] = $email;
        $_SESSION['name'] = $row['name'];




        header("Location: /dashboard/index.php");
    } else {
        $_SESSION['error'] = 1;
        header("Location: /dashboard/login.php");
    }
}

登录的前端与其他页面相同,区别在于会话的名称。

3 个答案:

答案 0 :(得分:0)

在您的第一个文件中,您还必须使用session_start()

您正在分配$_SESSION['succes_slider'] = 1;,但此文件中没有启动会话。 所以只需在第一个代码的第一行使用session_start();即可。

答案 1 :(得分:0)

在使用任何$ _SESSION之前,您应该使用session_start。此外,我看到你正在使用include,我建议你转移到require,因为如果你没有文件,它会触发错误。 另一个建议是在脚本中包含/要求它们之前检查文件是否存在。

<?php
// include "../../../config/config.php";
// if a file is missing include won't trigger an error
require "../../../config/config.php";

// always include session_start where you plan to use $_SESSION
session_start();

if (isset($_POST['submit-slider']))

答案 2 :(得分:0)

我认为你没有在文件上使用Session Started代码标签......

在您的要求之后添加:

session_start();