简体   繁体   English

如何通过Javascript将php会话从一页进行到另一页?

[英]How do I carry over a php session from one page to another via Javascript?

I have a form (form.php) that uses the following JS code in the header: 我有一个表单(form.php)在标头中使用以下JS代码:

<script type="text/javascript">
$(document).ready(function(){
$("a").click(function(){

$.post('test.php', function(data) {
        $('#test').html(data);
    });
    });
});
</script>

A have a link in form.php that once clicked I want a drop down to appear (without refreshing the page) eg "Display drop down". 在form.php中有一个链接,一旦单击,我希望下拉菜单显示(不刷新页面),例如“显示下拉菜单”。 The code in the drop down is managed in test.php. 下拉列表中的代码在test.php中进行管理。

Test.php pulls data in from another service. Test.php从另一个服务中提取数据。 In order to do get this data I use data that is kept in the session eg $_SESSION['data_that_is_sent_to_another_service']. 为了获得此数据,我使用了会话中保留的数据,例如$ _SESSION ['data_that_is_sent_to_another_service']。

I start the session in form.php but in order for the test.php to get the information from the other service I need to start a session at the start of test.php. 我在form.php中启动会话,但是为了使test.php从其他服务中获取信息,我需要在test.php的开头启动会话。

The code works, but I then get a warning saying: 该代码有效,但随后我得到一条警告:

Warning: session_start() [function.session-start]:
Cannot send session cache limiter - headers already sent
(output started at /..my directory.../test.php:1) in /...my directory.../hrdeals.php on line 4

How can I get this warning not to appear (without turning off warnings in PHP)? 如何使该警告不出现(不关闭PHP中的警告)?

Any help much appreciated. 任何帮助,不胜感激。

Gregor 格里高

PS - the JS might not be correct, but it's more the SESSION issue that I can solve PS-JS可能不正确,但是我可以解决更多的SESSION问题

session_start() must be called before outputing anything to the browser. 在向浏览器输出任何内容之前,必须先调用session_start() Check test.php and make sure there is no output. 检查test.php并确保没有输出。

  1. Javascript and PHP Sessions have nothing to do with one another. Javascript和PHP会话彼此无关。 Javascript lives in its little bubble (client side rendering/processing). JavaScript生活在它的小泡泡中(客户端渲染/处理)。 PHP lives in its bubble (server side processing/output). PHP处于泡沫中(服务器端处理/输出)。

  2. The error you get is (briefly) - headers already sent. 您收到的错误是(简短)-标头已发送。

This refers to http headers (not the head tag where you may have some javascript). 这是指http标头(而不是可能包含一些javascript的head标签)。 You can see the whole list here: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields 您可以在此处查看整个列表: http : //en.wikipedia.org/wiki/List_of_HTTP_header_fields

This error occurs when you start rendering web page stuff before outputting a header. 当您在开始输出标题之前开始呈现网页内容时,会发生此错误。 All headers must be completed first. 所有标头必须先完成。

Check that all echos and prints from php are occurring after session_start() occurs. 在session_start()发生之后,检查是否发生了所有来自php的回显和打印。 One tricky place that printing creeps in is when you end php tags and have anything there - even when it's at the end of a page. 当您结束php标记并在那里放有任何内容时,即使在页面的末尾,打印也会进入一个棘手的地方。 You don't need ending php tags at the end of your pages and even a space following one will cause this kind of problem. 您不需要在页面末尾使用php标记,甚至在页面末尾加一个空格也会引起这种问题。 The safest thing is to just remove all ending php tags at the bottom of your php files. 最安全的事情是只删除PHP文件底部的所有结尾php标签。

Another tricky thing is that this kind of error is also caused by a "Byte Order Mark" being output by your code editor. 另一个棘手的事情是,这种错误也是由代码编辑器输出的“字节顺序标记”引起的。 You can try opening, fixing and saving the page in just a simple plain-text editor and see if that fixes the problem. 您可以尝试使用简单的纯文本编辑器打开,修复和保存页面,看看是否可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM