PHP - Session already started - ignoring session_start (but doesn't do just that)

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-23 17:17:28

问题


Does anyone have an explanation for what is going on here?

I have a mainpage, on which I call session_start(). Then I have an PHP script, which is invoked using Ajax. If I don't put in session_start() there, it will not work. However, if I do, it works just fine, but displaying:

Notice: A session had already been started - ignoring session_start() in C:\xampp\htdocs\dsb\php\ds_acc.php on line 2

I probably have made a mistake somewhere, but isn't that a paradox? While saying, it ignores that session_start(), the script breaks when removing it. Thank you for your input.

Here the code, sorry if it is a bit obfuscated:

index.php:

<?php
  session_start();
  require_once("php/ds_acc.php");
?>

ds_acc.php:

<?php
session_start();

require_once("db_login.php");
require_once("permission.php");

if(check_permission("user"))
{   
    if(isset($_REQUEST["action"]))
    {
        switch($_REQUEST["action"])
        {
            case "add":
            add_user();
            break;
        }
    }
}else
{
    echo "error: session invalid";
}

function add_user()
{
}
?>

permission.php:

<?php
require_once("db_login.php");
/* Returns true if permission is granted, otherwise false. */

function check_permission($required_level)
{
    if(!isset($_SESSION["id"]))
    {
        /* Not logged in */
        return false;
    }

    /* reload banned */
    $con = get_con();
    $stmt = $con->prepare("SELECT banned FROM users WHERE id = ?");
    $stmt->bind_param("i", $_SESSION["id"]);
    $stmt->execute();
    $stmt->bind_result($banned);
    $stmt->fetch();
    $stmt->close();
    $con->close();

    if($banned == 1)
    {
        $_SESSION = array();
        session_destroy();
        return false;
    }


    if($_SESSION["permission"] == "admin")
    {
        return true;
    }

    switch($required_level)
    {
        case "user":
            return true;
        case "mod":
            return ($_SESSION["permission"] == "mod");
        default:
            return false;
    }
}
?>

At the time of copying, I now realized that it probably is connected to me including the ds_acc.php file to use it's functions, while also using it to post data via Ajax. I should do that in separate files.

(Well, nvm, stackoverflow does not like php starting tags apparently) Btw, took me a lot of time to format the code before it let me post it, because the first <? in one of my files was intended only 3 spaces ._.


回答1:


The error is, that I include the php file in my index page, and then also invoke it using ajax. So it will display the duplicate session error on the page, while it needs the session_start if called via Ajax.

Solution: Create a separate file, in which I put all the functions from ds_account.php. Include the new file in ds_acc.php and in the main page.




回答2:


Check if session is started 1st:

ds_acc.php:

<?php
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

require_once("db_login.php");
require_once("permission.php");



回答3:


session_start();

must only be executed once. just write it once on the index.php and remove it from the rest of the php files included on your index.php.



来源:https://stackoverflow.com/questions/26763687/php-session-already-started-ignoring-session-start-but-doesnt-do-just-that

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!