在写一个test.php文件的时候,里面有php代码和html代码,以前写过很多次了,恰好这次出问题了,百思不得其解。
先看代码:
1 <?php 2 header("content-type:text/plain;charset=utf-8"); 3 date_default_timezone_set("Asia/Shanghai"); 4 //connect db; 5 $conn = mysql_connect("localhost","root","") or die(mysql_error()); 6 $link = mysql_select_db("test"); 7 mysql_query("set names utf8"); 8 9 10 $a = 1; 11 function abc(){ 12 global $a; 13 echo $a; 14 } 15 abc(); 16 ?> 17 18 <form action="apiforios.php" method="POST"> 19 <input name="openid" value="01234"> 20 <input name="password" value="01234"> 21 <input name="method" value="resetPassword"> 22 <input name="device" value="d"> 23 <input type="submit" value="提交"> 24 </form>
发现下面这段html代码直接就把代码打印出来了,并没有显示表单。
但是当我把所有php代码删除了它就显示了。
后来无意中发现问题出现在:header("content-type:text/plain;charset=utf-8");这里。其中text/plain应该是text/html,因为前者是无格式的,相当于记事本,后者才算html格式。