PHP:session_start()在服务器上不起作用,但是在本地主机上?

时间:2018-12-06 13:28:07

标签: php ajax database forms session

我现在搜索了几个小时,但找不到任何解决方案。我希望你能帮助我。

我创建了一个网站并使用xampp对其进行了测试,并且一切正常,但是现在我将其上传到服务器上,并且我意识到var now = Date.now(); var 24hoursago = now - ...; var ref = firebase.database().ref("yourSpecificNode"); var query = ref.orderByChild("timestamp").startAt(24hoursago).end(now); query.once("value").then(function(snapshot) { snapshot.forEach(function(user) { user.ref.update({ yourSpecificProperty: "0" }); }); }); 不起作用。

这是我的index.php的代码:

session_start()

如果我加载页面,将不会创建会话ID,但是如果我在localhost上测试完全相同的代码,它将起作用。 我认为服务器出现问题,并上传了一个包含以下代码的文件:

<?php 
            session_start();
            echo session_status()."<br>";
            echo "SessionID: ".session_id();

        error_reporting(E_ALL & ~E_NOTICE); 

?>

<html>


    <head>
        <link rel="stylesheet" type="text/css" href="stylesheet.css" media="screen" />
        <meta charset="UTF-8" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
              // Add smooth scrolling to all links
              $("a").on('click', function(event) {

                // Make sure this.hash has a value before overriding default behavior
                if (this.hash !== "") {
                  // Prevent default anchor click behavior
                  event.preventDefault();

                  // Store hash
                  var hash = this.hash;

                  // Using jQuery's animate() method to add smooth page scroll
                  // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
                  $('html, body').animate({
                    scrollTop: $(hash).offset().top
                  }, 800, function(){

                    // Add hash (#) to URL when done scrolling (default click behavior)
                    window.location.hash = hash;
                  });
                } // End if
              });

              $(".loginField").click(function() {
                  $(".loginDetails").toggle("slow");
              });

              $(".profilName").click(function() {
                  $(".profilMenu").toggle("slow");
              });

              $("#middleLoginButton").click(function() {
                  $(".middleLoginForm").toggle("slow");
              });
            });
        </script>
    </head>

    <body>
        <!-- getting UserData, if available -->
        <?php

            //delete Session Data, if logout
            if (isset($_GET['logout']) && $_GET['logout'] == 1) {
               echo "SessionID Logout:".session_id();
               session_destroy();
               //header("Location:https://www.whocando.eu");
            }

            function autoload ($className) {
                if (file_exists('classes/'.$className.'.php')) {
                    require 'classes/'.$className.'.php';
                }
            }

            spl_autoload_register("autoload");

            if ($_GET['falsePassword'] == 1) {
                $falsePassword = 1;
            }

            if ((empty($_POST['userName']) || empty($_POST['password'])) && empty($_POST['registration'])) {
            // after Login Check! 
            } elseif (isset($_POST['userName']) && isset($_POST['password']) && !isset($_POST['registration'])) {
                $loginCheck = new loginParser();
                $userID = $loginCheck->loginChecker($_POST['userName'],$_POST['password']);
                $_SESSION['userID'] = $userID;

                $abfrage = new dbQuery("SELECT ID,name, firstName FROM db764570417.userdata WHERE ID = $userID");
                $userName = $abfrage->fetchData('ID','name');
                $userFirstName = $abfrage->fetchData('ID','firstName');

            // already logged in Check!
            } elseif (isset($_SESSION['userID'])) {
                $userID = $_SESSION['userID'];

                $abfrage = new dbQuery("SELECT ID,name, firstName FROM db764570417.userdata WHERE ID = $userID");
                $userName = $abfrage->fetchData('ID','name');
                $userFirstName = $abfrage->fetchData('ID','firstName');
            // after Registration Check!
            } elseif (isset($_POST['registration'])) {
                $name = $_POST['name'];
                $vorName = $_POST['vorname'];
                $email = $_POST['email'];
                $uni = $_POST['uni'];
                $geburtstag = $_POST['gebDatum'];
                $password = $_POST['passwort'];
                $confirmedPassword = $_POST['confirmPasswort'];
                $gebDatum = date("Y-m-d",strtotime($geburtstag));
                $abfrageEmail = new dbQuery("SELECT ID,email FROM db764570417.logindata");
                $userEmails = $abfrageEmail->fetchData('ID','email');

                if ($password != $confirmedPassword) {
                    header("Location:https://www.whocando.eu/registration.php?fault=passwordNotMatched&name=".$name."&vorname=".$vorName."&email=".$email."&uni=".$uni."&gebDatum=".$geburtstag);
                } elseif (in_array($email,$userEmails)) {
                    header("Location:https://www.whocando.eu/registration.php?fault=emailAlreadyUses&name=".$name."&vorname=".$vorName."&email=".$email."&uni=".$uni."&gebDatum=".$geburtstag);
                } elseif (empty($name)) {
                    header("Location:https://www.whocando.eu/registration.php?fault=nameMissing&name=".$name."&vorname=".$vorName."&email=".$email."&uni=".$uni."&gebDatum=".$geburtstag);
                }elseif (empty($vorName)) {
                    header("Location:https://www.whocando.eu/registration.php?fault=vorNameMissing&name=".$name."&vorname=".$vorName."&email=".$email."&uni=".$uni."&gebDatum=".$geburtstag);
                } elseif (empty($email)) {
                    header("Locationhttps://www.whocando.eu/:registration.php?fault=emailMissing&name=".$name."&vorname=".$vorName."&email=".$email."&uni=".$uni."&gebDatum=".$geburtstag);
                } elseif (empty($uni)) {
                    header("Location:https://www.whocando.eu/registration.php?fault=uniMissing&name=".$name."&vorname=".$vorName."&email=".$email."&uni=".$uni."&gebDatum=".$geburtstag);
                }elseif (empty($geburtstag)) {
                    header("Locationhttps://www.whocando.eu/:registration.php?fault=gebMissing&name=".$name."&vorname=".$vorName."&email=".$email."&uni=".$uni."&gebDatum=".$geburtstag);
                } elseif (empty($password)) {
                    header("Location:https://www.whocando.eu/registration.php?fault=passwordMissing&name=".$name."&vorname=".$vorName."&email=".$email."&uni=".$uni."&gebDatum=".$geburtstag);
                } elseif (empty($confirmedPassword)) {
                    header("Location:https://www.whocando.eu/registration.php?fault=confirmedPasswordMissing&name=".$name."&vorname=".$vorName."&email=".$email."&uni=".$uni."&gebDatum=".$geburtstag);
                } else {
                    include ('dbConnection.php');

                    $date = date("Y-m-d H:i:s",time());
                    $hashedPassword = password_hash($password, PASSWORD_DEFAULT);
                    $sqlCode = "INSERT INTO db764570417.logindata (name,email,password)
                      VALUES (?,?,?)";

                    $userNameDB = $vorName."".$name;

                    $statement = $mysqli->prepare($sqlCode);
                    $statement->bind_Param('sss',$userNameDB,$email,$hashedPassword);
                    $statement->execute();
                    $newUserId = $mysqli->insert_id;

                    $_SESSION['userID'] = $newUserId;

                    $sqlCode = "INSERT INTO db764570417.userdata (ID,name,firstName,firstLogin,lastLogin,birthDate,email,university)
                          VALUES (?,?,?,?,?,?,?,?)";

                    $statement = $mysqli->prepare($sqlCode);
                    $statement->bind_Param('isssssss',$newUserId,$name,$vorName,$date,$date,$gebDatum,$email,$uni);

                    $statement->execute();
                    //header("Location:index.php");

                    $userID = $newUserId;
                    $userFirstName[$userID] = $vorName;
                    $userName[$userID] = $name;
                }
            }

        ?>

我现在完全困惑了,不知道我犯了什么错误。

有人可以帮我吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

执行以下代码并找到session.save_path

<?php
    phpinfo();
?>

只需为此路径添加正确的权限