1

我有一个写每个业务页面的 fwrite,但我想要它,以便它只在您使用有效会话访问 url 时显示页面的 html,所以我写了 fwrite($fp,"

但是,当它写入页面时,它会写入 this 。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);
}

这是它写入的页面

    <?php session_start();if() { ?><!DOCTYPE html>
<html><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>
                 <body>
                 <div id='header'> </div><img src='wc.png' id='wc'><table id='accountinfo'> 
                 <tr> <td id='accountinfotd1'> </td> </tr> 
                 <tr> <td id='accountinfotd2'>test </td> </tr> 
                 <tr> <td id='accountinfotd3'></td> </tr> 
                 </table><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></body></html><?php } else { echo "User not logged in";} ?>
4

1 回答 1

2

原因是因为您使用的是以下行:

fwrite($fp, "if(isset({$_SESSION['id']})) { ?");

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

fwrite($fp, 'if(isset($_SESSION["id"])) { ?');

您还应该删除会话变量上的大括号,除非您希望您的 php 在重新解析时出错。

这一切都假设您在文件中想要的结果是:

if(isset($_SESSION["id"])) { ?
于 2013-07-27T21:41:29.643 回答