PHP的:$ _SESSION似乎并没有被置1。任何想法? [英] PHP: $_SESSION doesn't seem to get set. Any idea?

查看:125
本文介绍了PHP的:$ _SESSION似乎并没有被置1。任何想法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过这个SO Q / A在搜索时,为什么我的会议将不会被置了。

在$ _GET设置$ _SESSION将无法正常工作

我真的不明白这是否适用于我的情况。在这里,它去。

的index.php

 < PHP
在session_start();

如果(使用isset($ _ SESSION [认证])!)$ _SESSION [认证] = FALSE;
?>
!< D​​OCTYPE HTML PUBLIC -  // W3C // DTD HTML 4.01 // EN>
< HTML>
< HEAD>
...
 

然后,我打电话,当用户进行身份验证自己以下。

authenticate.php

 < PHP
require_once数据/ data_access.php;

$ USERNAME =;
$密码=;

如果(使用isset($ _ REQUEST [username的]))$ USERNAME = $ _REQUEST [username的]。
如果(使用isset($ _ REQUEST [密码]))$密码= $ _REQUEST [密码];

$ isAuthentic = isAuthenticUser($用户名,密码$)
$ _SESSION [认证] = $ isAuthentic;
回声$ isAuthentic;
?>
 

我也有为了创建按钮部分中的用户是在该认证-东西检查每5秒

authenticated.php

 <?PHP的回声使用isset($ _ SESSION [认证])及和放大器; $ _SESSION!=假? '真假'; ?>
 

和我的Ajax调用设置:

my_project.js

  $(文件)。就绪(函数(){startAuthenticationChecking();});

功能startAuthenticationChecking(){
    的setInterval(函数(){ajaxSession(authenticated.php);},5000);
}

功能ajaxSession(actionURL){
    VAR认证= FALSE;
    $阿贾克斯({
        异步:假的,
        网址:actionURL,
        成功:函数(认证){
            警报(认证);
            如果(认证){
                如果(。(#addNewAtvButtonDiv)是(:空))
                    $(#addNewAtvButtonDiv)增加(<按钮的ID ='newAtvButton'> Inscrire未暴发户VTT连接inventaire< /按钮>中)。
                如果(。(#addNewSledButtonDiv)是(:空))
                    $(#addNewSledButtonDiv)增加(<按钮的ID ='newSledButton'> Inscrire联合国努维尔UTT连接inventaire< /按钮>中)。
                如果(。(#addNewUtvButtonDiv)是(:空))
                    $(#addNewUtvButtonDiv)增加(<按钮的ID ='newUtvButton'> Inscrire UNE新式motoneige< /按钮>中)。
                $(按钮)按钮()。
            } 其他 {
                $(#addNewAtvButtonDiv)儿童()删除()。
                $(#addNewSledButtonDiv)儿童()删除()。
                $(#addNewUtvButtonDiv)儿童()删除()。
            }
        }
    });
}
 

  

我的问题

虽然我设置 $ _ SESSION [认证] = FALSE 在session_start()之后; ,当在 ajaxSession()要求通过 authenticated.php 用户是否经过验证,它总是返回。通过认证后,事件 authenticate.php 用正确的凭据。

相关问题:<一href="http://stackoverflow.com/questions/19110684/php-ajax-how-to-show-hide-div-on-session-variable-value"><$c$c>PHP / AJAX:如何显示在$ _SESSION变量值/隐藏DIV

任何帮助的欢迎!我一直在这几个日日夜夜最近,仍然在。

谢谢!

解决方案

请确保使用 $ _ SESSION 变量的所有页面都有在session_start() ; 任何输出之前声明

看起来 authenticate.php 可是没有在session_start(); 宣布,但使用低于会话请求

  

$ _ SESSION [认证] = $ isAuthentic;

除非你已包括这将导致问题/所需的文件中的 authenticate.php ,然后父文件具有在session_start(); 声明

N.B:

在session_start();要么创建一个会话或恢复之一。它要求使用会话变量在每一页上。

php.net

  

在session_start()创建一个会话或恢复当前的基础上通过浏览器通过GET或POST请求通过,或者通过会话标识符。

I got through this SO Q/A while searching why my session wouldn't get set.

On $_GET set $_SESSION won't work

I don't really get whether this applies to my case. Here it goes.

index.php

<?php 
session_start();

if (!isset($_SESSION["authenticated"])) $_SESSION["authenticated"] = false; 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
...

Then, I call the following when the user authenticates himself.

authenticate.php

<?php
require_once "data/data_access.php";

$userName = "";
$password = "";

if (isset($_REQUEST["userName"])) $userName = $_REQUEST["userName"];
if (isset($_REQUEST["password"])) $password = $_REQUEST["password"];

$isAuthentic = isAuthenticUser($userName, $password);
$_SESSION["authenticated"] = $isAuthentic;
echo $isAuthentic;
?>

I also have this authentication-thing checked every 5 seconds in order to create the buttons for the section the user is in.

authenticated.php

<?php echo isset($_SESSION["authenticated"]) && $_SESSION != false ? 'true' : 'false'; ?>

and my Ajax call setup:

my_project.js

$(document).ready(function() { startAuthenticationChecking(); });

function startAuthenticationChecking() { 
    setInterval(function() { ajaxSession("authenticated.php"); }, 5000);
}

function ajaxSession(actionURL) {
    var authenticated = false;
    $.ajax({
        async: false,
        url: actionURL,
        success: function(authenticated) {
            alert(authenticated);
            if (authenticated) {
                if (("#addNewAtvButtonDiv").is(":empty"))
                    $("#addNewAtvButtonDiv").add("<button id='newAtvButton'>Inscrire un nouveau VTT en inventaire</button>");
                if (("#addNewSledButtonDiv").is(":empty"))
                    $("#addNewSledButtonDiv").add("<button id='newSledButton'>Inscrire un nouvel UTT en inventaire</button>");
                if (("#addNewUtvButtonDiv").is(":empty"))
                    $("#addNewUtvButtonDiv").add("<button id='newUtvButton'>Inscrire une nouvelle motoneige</button>");
                $("button").button();
            } else {
                $("#addNewAtvButtonDiv").children().remove();
                $("#addNewSledButtonDiv").children().remove();
                $("#addNewUtvButtonDiv").children().remove();
            }
        }
    });
}

My Problem

Though I set $_SESSION["authenticated"] = false right after the session_start();, when the ajaxSession() asks whether a user is authenticated through authenticated.php, it always returns 'false'. Event after an authentication using authenticate.php with proper credentials.

Related question: PHP / Ajax : How to show/hide DIV on $_SESSION variable value?

Any help welcome! I've been working on this for a few nights and days lately, and still am.

Thanks!

解决方案

Make sure that all pages that use $_SESSION variables have session_start(); declared before any output.

It looks like authenticate.php doesnt have session_start(); declared, but uses a session request below.

$_SESSION["authenticated"] = $isAuthentic;

This will cause problems unless you have included/required the file authenticate.php, and the parent file has the session_start(); declared.

N.B:

session_start(); will either create a session or resume one. It is required on every page that uses session variables.

php.net

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.

这篇关于PHP的:$ _SESSION似乎并没有被置1。任何想法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆