php如何在header()函数之前输出内容?

php如何在header()函数之前输出内容?,第1张

PHP 中 header()函数的作用是给客户端发送头信息

什么是头信息?

这里只作简单解释,详细的自己看http协议。

在 HTTP协议中,服务器端的回答(response)内容包括两部分:头信息(header) 和 体内容,这里的头信息不是HTML中的<head></head>部分,同样,体内容也不是<BODY>< /BODY>。头信息是用户看不见的,里面包含了很多项,包括:服务器信息、日期、内容的长度等。而体内容就是整个HTML,也就是你所能看见的全 部东西。

头信息有什么用呢?

头信息的作用很多,最主要的有下面几个:

1、跳转:当浏览器接受到头信息中的 Location: xxxx 后,就会自动跳转到 xxxx 指向的URL地址,这点有点类似用 js 写跳转。但是这个跳转只有浏览器知道,不管体内容里有没有东西,用户都看不到。

2、指定网页的内容: 同样一个XML文件,如果头信息中指定:Content-type: application/xml 的话,浏览器会将其按照XML文件格式解析。但是,如果头信息中是:Content-type: text/xml 的话,浏览器就会将其看作存文本解析。(浏览器不是按照扩展名解析文件的)

不知道大家有没有注意,有些时候在一些网站下载东西,点下载连接以后,结果浏览器将这个附件当成网页打开 了,里面显示的都是乱码,这个问题也和头信息有关。有时候浏览器根据Content-type 来判断是打开还是保存,这样有时就会判断错误(主要是网站设计者忘记写Content-type)。其实,还有一个可以来指定该内容为附件、需要保存,这 个就是:Content-Disposition: attachment; filename="xxxxx"

// 指定内容为附件

header('Content-Disposition: attachment; filename="downloadedpdf"');

// 打开文件,并输出

readfile('originalpdf');

兄弟,你要注意区分好php和JS的区别啊。帮你搞定了。

//wp_442057835php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 10 Transitional//EN" "http://wwww3org/TR/xhtml1/DTD/xhtml1-transitionaldtd">

<html xmlns="http://wwww3org/1999/xhtml">

<head>

<title>Ajax Get Picture</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<meta http-equiv="Content-Language" content="zh-CN" />

</head>

<body>

<script>

function last_pic() {

userid = documentgetElementById("user_pic")getAttribute("imgid"); //使用imgid存储当前照片id

xmlHttp=GetXmlHttpObject();

if (xmlHttp==null) {

alert ("Browser does not support HTTP Request");

return null;

}

var q="1";

var url="wp_442057835_processphpq="+q+"&u="+userid+"&sid="+Mathrandom();

xmlHttponreadystatechange=stateChanged;

xmlHttpopen("GET",url,true);

xmlHttpsend(null);

}

function next_pic() {

userid = documentgetElementById("user_pic")getAttribute("imgid");

xmlHttp=GetXmlHttpObject();

if (xmlHttp==null) {

alert ("Browser does not support HTTP Request");

return null;

}

var q="2";

var url="wp_442057835_processphpq="+q+"&u="+userid+"&sid="+Mathrandom();

xmlHttponreadystatechange=stateChanged;

xmlHttpopen("GET",url,true);

xmlHttpsend(null);

}

function stateChanged() {

if (xmlHttpreadyState==4 || xmlHttpreadyState=="complete"){

var nuserid=xmlHttpresponseText;

var newImage = documentgetElementById("user_pic");

newImagesetAttribute("src", "user_pic/"+nuserid+"jpg");

newImagesetAttribute("imgid", nuserid);

}

}

function GetXmlHttpObject(){

var request;

var browser = navigatorappName;

if(browser == "Microsoft Internet Explorer"){

var arrVersions = ["MicrosoftXMLHTTP","MSXML2XMLHttp40",

"MSXML2XMLHttp30","MSXML2XMLHttp50"];

for(var i=0;i < arrVersionslength;i++){

try{

request = new ActiveXObject(arrVersions[i]);

return request;

}catch(exception){

//忽略,继续

}

}

}else{

request = new XMLHttpRequest();

return request;

}

}

</script>

<div id="pic_show">

<a href="javascript:;" onclick="last_pic(); return false;" >上一张</a>

<img src="<php echo "user_pic/1jpg" >" alt="修改头像" hspace="7" id="user_pic" width="200px" height="100px" imgid="1"/>

<a href="javascript:;" onclick="next_pic(); return false;" >下一张</a>

</body>

</html>

//wp_442057835_processphp

<php

header("content-type:textml;charset=utf-8");

include("dbphp");

//echo $_GET["q"];

//echo $_GET["u"];

$userid=$_GET["u"];

if($_GET["q"]==1)

{

$lquery=mysql_query("SELECT FROM usermember WHERE userid < $userid ORDER BY userid DESC LIMIT 1 ");

$last_user =mysql_fetch_object($lquery);

$last_userid=$last_user->userid;

if(empty($last_userid)) $last_userid = $userid;

echo $last_userid;

}

if($_GET["q"]=="2")

{

$nquery=mysql_query("SELECT FROM usermember WHERE userid > $userid ORDER BY userid ASC LIMIT 1 ");

$next_user =mysql_fetch_object($nquery);

$next_userid=$next_user->userid;

if(empty($next_userid)) $next_userid = $userid;

echo $next_userid;

}

首先从网上下载出WordPress的插件Wp-syntax。

在编辑文章时,使用HTML的编辑方式插入以下代码<pre lang=”LANGUAGE” line=”0″>//“line为1时表示显示行号”</pre>

中间插入你的代码即可,LANGUAGE改为语言类型,例如php、java。line为0不显示行号,为1时显示

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/11675795.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存