2012-03-06 44 views
1

当我使用下面提供的代码时,一切正常,除非在SESSION中有一个撇号或其他html特殊字符,我试图传递值。我试过htmlspecialchars()和htmlentities()都没有成功。请帮忙。 感谢, 詹姆斯POST另一个页面上的SESSION变量

<?php 
<form action='ashlyBlogBig2.php' method='POST'> 
    <input type='hidden' name='title' value='{$_SESSION['title']}'/> 
    <input type='hidden' name='time' value='{$_SESSION['time']}'/> 
    <input type='hidden' name='blog' value='{$_SESSION['blog']}'/> 
    <input type='submit' name='to you' class='productButtons' value='Read On. . . .'> 
</form> "; 
?> 
+0

请显示一些样品数据,预期产量和实际产量。并请定义什么“工作正常,除非”意味着确切。 – deceze 2012-03-06 02:23:43

回答

4

使用ENT_QUOTES作为第二个参数去htmlentities(),可以肯定,单,双引号的变量中的编码。

echo "<input type='hidden' name='blog' value='" . htmlentities($_SESSION['blog'], ENT_QUOTES) . "'/>" 

由于htmlentities()是一个函数调用,它不能在双引号内的字符串被内插的方式的变量,数组元素,或对象属性可以。您必须关闭当前打开的字符串并在函数调用的返回中连接。

ENT_QUOTES标记为htmlentities()对单引号和双引号进行编码,从而生成适用于HTML属性(已引用)的字符串。

+0

我刚试过这种方法,它工作!为什么添加的双引号和连接使这项工作成为现实?感谢帮助。 – James30 2012-03-06 18:24:45

+0

@JamesHowardHemsley我在上面加了一点解释。 – 2012-03-06 18:30:27

+0

再次感谢MIchael – James30 2012-03-07 01:59:17