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 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?