Php Fwrite未写入if语句内部

Php Fwrite未写入if语句内部,php,Php,我有一个写每个企业的页面的fwrite,但我希望它只显示页面的html,当你用一个有效的会话转到url,所以我写了 fwrite($fp,” 但是,当它写入页面时,它会写入以下内容。isset语句将从if语句中删除 <?php $recid = $_POST['recid']; $username = $_POST['name']; $email = $_POST['email']; $ownername = $_POST['ownername']; include ("con

我有一个写每个企业的页面的fwrite,但我希望它只显示页面的html,当你用一个有效的会话转到url,所以我写了 fwrite($fp,” 但是,当它写入页面时,它会写入以下内容。isset语句将从if语句中删除

    <?php

$recid = $_POST['recid'];
$username = $_POST['name'];
$email = $_POST['email'];
$ownername = $_POST['ownername'];

include ("connection.php");


$result = $db->query("UPDATE users SET verified='y' WHERE recid='$recid'"); 

$doctype      = "<!DOCTYPE html>";
$htmlOpen     = "<html>";
$head         = "<head> <meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>
                 <script src='http://code.jquery.com/jquery-1.9.1.min.js'> </script>
                 <script src='http://code.jquery.com/ui/1.10.2/jquery-ui.js'> </script> 
                 <script src='Scripts/dropdown.js'> </script> 
                 <link rel='stylesheet' href='CSS/businesspages.css'> 
                 <link rel='stylesheet' href='http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css'> 
                 <script src='Scripts/postdata.js'> </script>
                 </head>";

$bodyOpen     = "<body>";
$header       = "<div id='header'> </div>";
$wcImage      = "<img src='wc.png' id='wc'>";
$accountTable = "<table id='accountinfo'> 
                 <tr> <td id='accountinfotd1'> </td> </tr> 
                 <tr> <td id='accountinfotd2'>{$username}</td> </tr> 
                 <tr> <td id='accountinfotd3'>{$ownername}</td> </tr> 
                 </table>";

$linksTable   = "<table id='links'> 

                <tr> <td> <input type='text' id='post' placeholder='Post' name='post'> </td> </tr>

                <tr> <td> <img src='images/profile.jpg' id='profile'> </td> </tr> 

                <tr> <td> <div id='profileDiv'>
                <table>
                <tr> 
                <td> <input type='password' id='changePSW' name='changePSW' placeholder='Change Password' tabindex=2> </td>
                <td> <input type='password' id='verifyPSW' name='verifyPSW' placeholder='Verify Password' tabindex=3> </td>
                <td> <input type='image' id='changePSWBtn' src='images/changeBtn.jpg' name='changePSWBtn'> </td>
                </tr>
                <tr> 
                <td> <input type='text' id='changeLA' name='changeLA' placeholder='Latitude' tabindex=6> </td>
                <td> <input type='text' id='changeLO' name='verifyPSW' placeholder='Longatude' tabindex=7> </td>
                <td> <input type='image' id='changeLLBtn' src='images/changeBtn.jpg' name='changeLLBtn'> </td>
                </tr>
                </table>
                </div> </td> </tr> 

                <tr> <td> <img src='images/legal.jpg' id='legal'> </td> </tr> 

                <tr> <td> <div id='legalDiv'>
                <a href='Legal_Documents/termsofuseweb.pdf' target='_blank'> Web Terms Of Use </a> </br>
                <a href='Legal_Documents/termsofuseios.pdf' target='_blank'> iOS Terms Of Use </a> </br>
                <a href='Legal_Documents/enduserlisenceagreement.pdf' target='_blank'> End Of User Licensee </a>
                </div> </td> </tr> 

                <tr> <td> <img src='images/help.jpg' id='help'> </td> </tr> 

                <tr> <td> <div id='helpDiv'> 
                <p> If you need help you can contact Phil Pilon or Matt Muehlemann info@wolfeboroconnection.com </p>
                </div> </td> </tr> 

                </table>";

$bodyClose    = "</body>";
$htmlClose    = "</html>";
$isset = "{$_SESSION['id']}";

$filename = str_replace(" ","_", trim($username) ); 
mkdir("Business_Pages/" . $filename."/");

if( $fp = fopen("Business_Pages/" . $filename . "/" . $filename . ".php", "w") )
{
  fwrite($fp,"<?php session_start();");
  fwrite($fp, "if(isset({$_SESSION['id']})) { ?");
  fwrite($fp, $doctype.$htmlOpen.$head.$bodyOpen.$header.$wcImage.$accountTable.$linksTable.$bodyClose.$htmlClose);
  fwrite($fp,"<?php } else { echo \"User not logged in\";} ?>");
  fclose($fp);
}

原因是您正在使用以下行:

fwrite($fp, "if(isset({$_SESSION['id']})) { ?");
因为您使用双引号,这意味着会话值
{$\u session['id']}
将由PHP解析,然后通过fwrite写入文件。如果您将引号更改为单引号,则不会出现此问题,因为PHP将单独保留单引号字符串-即不解析变量:

fwrite($fp, 'if(isset($_SESSION["id"])) { ?');
您还应该删除会话变量上周围的大括号,除非您希望php在重新解析时出错

这都是假设您希望文件中的结果是:

if(isset($_SESSION["id"])) { ?

你能缩小到一个小片段来重现这个例子吗?@user2354835没问题:)这正是我想要的结果谢谢!!让我发疯了几分钟;)+1
if(isset($_SESSION["id"])) { ?