2013-12-22 00:27
浏览 178

如何在Ajax请求后访问$ _SESSION变量?

I have a page where people can login and their login request is sent with jQuery's $.ajax to a processing page. On that page all of the errors collected during the login attempt are stored in $_SESSION['error'].

How can I access this variable to alert users of the errors after the attempt is made. I know how to access a session variale, I mean how do I access a php variable without reloading the page. The only way I currently am aware of is to inject them on page load, but there must be some way to access them with JavaScript after they have changed due to an Ajax request.

图片转代码服务由CSDN问答提供 功能建议

我有一个页面,人们可以登录,他们的登录请求与jQuery的$ .ajax一起发送到处理页面。 在该页面上,登录尝试期间收集的所有错误都存储在$ _SESSION ['error']中。

如何在尝试后访问此变量以提醒用户错误。 我知道如何访问会话变量,我的意思是如何在不重新加载页面的情况下访问php变量。 我目前唯一知道的方法是在页面加载时注入它们,但是由于Ajax请求而在更改之后必须有一些方法可以使用JavaScript访问它们。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doudou3935 2013-12-22 00:49

    Unfortunately you cannot directly access the PHP $_SESSION[] array from JavaScript. This is by design, because one of the major points of using a Session, is that the data is saved on the server side, unmodifiable by the end user, and therefore your Javascript.

    What you could do however, is include the error list in your ajax response. Chances are you are doing an ajax request that returns a 'json' type format. Just include your error list in your json returned, and use the JavaScript to display them. I recommend not sharing the entire $_SESSION array, since it could give away some key information about the interworkings of your PHP.

    Also, if you are not currently doing an ajax request that expects json, back, then consider switching it up so that you are expecting and returning json. You can provide much more hidden data this way, instead of just some HTML or text.

    Hope this helps

    解决 无用
    打赏 举报

相关推荐 更多相似问题