开发者

PHP session and multiple mixed PHP/HTML sections in one page

开发者 https://www.devze.com 2023-01-08 16:04 出处:网络
If I have a page with multiple <?php ...?> sections interspresed with pure HTML sections. I notice that a $_SESSION varible set in one <?php ...?> section is not available in another on开发

If I have a page with multiple <?php ... ?> sections interspresed with pure HTML sections. I notice that a $_SESSION varible set in one <?php ... ?> section is not available in another on开发者_如何学运维 the same page.

So, what's the best practise?

1) call session_start() as the first line of each <?php ... ?> section?

2) only have one <?php ... ?> section which covers the whole page? If so, I have to wrap each HTML section in echo, which is annoying of they are HTML form elements. Maybe heredoc them?

It's my first time to try this sort of thing, but I am not the first one to do so - what's the accepted best practise?


Edit: Aplogies, my stupid fault. One of the sections PHP started with <? and not <?php


If I have a page with multiple sections interspresed with pure HTML sections. I notice that a $_SESSION varible set in one section is not available in another on the same page.

The sections of php tags <?php ... ?> have nothing to do with session. Make sure that you put:

session_start()

on top of your page.


As long as you set the header before doing any output, you shouldn't have any issues with the session (as the function session_start() also set the header).

There is really no problem having multiple PHP sections on a page. But I would highly recommend, to do all the logic (reading from database, processing form data) in a separate file OR in the first section.

0

精彩评论

暂无评论...
验证码 换一张
取 消