dongyongju9560 2011-11-03 13:01
浏览 41
已采纳

是否有符合标准的方法来启动PHP会话并在一个include()语句中回显JavaScript脚本?

I have two scripts that I call with two PHP include() calls. The first starts a session / sets cookies, the second loads one of two JavaScript scripts. To keep things valid, I've been using the two calls but I'd like to just combine them into one.

Current setup (simplified):

<? include "session.php" ?>
<!DOCTYPE HTML>
<html>
<head>
<? include "scripts.php" ?>
 ...

What I'd like:

<? include "session_and_scripts.php" ?>
<!DOCTYPE HTML>
<html>
<head>
...

But it's invalid markup. Now if it really doesn't matter, I'd like to do it this way. If there are serious repercussions, then I'm thinking of just echoing a DOCTYPE in the included PHP file, which I'd rather not do.

So which is better: echo the DOCTYPE, use include() twice, or use include() once and have invalid markup?

EDIT - The whole script (session and javascript) should ideally be fully implementable with one line of code (e.g. the one include())

  • 写回答

2条回答 默认 最新

  • douyan4958 2011-11-03 13:16
    关注

    Use ob_start at first to avoid problems with session_start

    <?php ob_start();?>
    <!DOCTYPE HTML>
    <html>
    <head>
    <?php include "session_and_scripts.php"; ?>
    

    A way that uses only 1 1file and no additional instructions:

    <?php include "session_and_scripts.php" ?>
    <!-- more head-stuff-->
    </head>
    <body>
    <!--more content-->
    

    session_and_scripts.php should do the following:

    <?php
      //do the session stuff
    ?>
    <!DOCTYPE HTML>
    <html>
    <head>
    
    <script type="text/javascript">
      //some javascript
    </script>
    

    (But I would'nt say it's a good approach)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题