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 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站