今天遇到一个问题,运行下面代码时报错。
<?php session_start(); if(!isset($_SESSION['user']) || null === $_SESSION['user']) { header('location:login.php'); exit; } ?>
错误信息为:
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at D:\PHPProjects\agilelite\index.php:1) in D:\PHPProjects\agilelite\index.php on line 2 Warning: Cannot modify header information - headers already sent by (output started at D:\PHPProjects\agilelite\index.php:1) in D:\PHPProjects\agilelite\index.php on line 6
为了解决这个问题,研究一下exit这个函数的用法。
return、break和contiue是语言结构,就如同if语句之类的,但是exit却是个函数。exit函数的作用是输出一则消息并且终止当前脚本。
如果一段文本中包括多个以 ?>结束的脚本,则exit退出当前所在脚本。
比如一篇php文本包括一下代码,则输出为 NowaMagic。
<?php echo "Hello"; exit; ?> <?php echo "NowaMagic"; ?>
exit函数语法格式如下(void表示没有返回值):
void exit ([ string $status ] ) void exit ( int $status )
如果status是一段字符串,这个函数在脚本退出前打印status。
如果status是一个整数,这个整数会被作为退出状态。退出状态应该从0到254,退出状态255被PHP保留并禁止使用。状态0被用来表示成功的终止程序。
在header跳转后使用exit
PHP的header可以输出http头部信息,前提是,在header之前不能输出任何内容,因为所有的内容都作为http的body输出给客户端了,一旦有body的内容,就不可能再添加任何head部分的信息。
header函数的一个常用的方式就是用来做转向,redirect。
比如,我需要转到某个地址,只要执行下述代码:
<?php header('Location: http://www.bkjia.com/'); ?>
浏览器会收到一个302的http状态码,告诉他这个内容已经被转移了。
重要的是,php会在调用header函数后,继续执行后面的代码,你可以用我的代码亲自试验一下:
<?php header('Location: http://www.bkjia.com/'); $fp = fopen('header.txt', 'w+'); fwrite($fp, date('Y-m-d H:i:s')); ?>
肯定会执行到fwrite函数的,打开header.txt文件,检查时间!
对此,解决的办法是,要在每一个header函数后面加上exit,保证当前页面停止执行,进而转向location指定的地址。
为了避免到处都是exit,可以写一个专门用作转向的函数,比如:
<?php function DoRedirect($strUrl) { header('Location: ' . $strUrl); exit; } ?>
另,为了避免在header之前输出body的内容,许多php框架都采用了php页面尾部不写?>的方式,因为有些人总习惯在?>后面添加一个换行,这真的是个不好的习惯。

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

使用依赖注入(DI)的原因是它促进了代码的松耦合、可测试性和可维护性。1)使用构造函数注入依赖,2)避免使用服务定位器,3)利用依赖注入容器管理依赖,4)通过注入依赖提高测试性,5)避免过度注入依赖,6)考虑DI对性能的影响。

phperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovesponsemetimes.2)优化

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

TOOPTIMIZEPHPAPPLICITIONSFORPERSTORANCE,USECACHING,数据库imization,opcodecaching和SererverConfiguration.1)InlumentCachingWithApcutCutoredSatfetchTimes.2)优化的atabasesbasesebasesebasesbasesbasesbaysbysbyIndexing,BeallancingAndWriteExing

依赖性注射inphpisadesignpatternthatenhancesFlexibility,可检验性和ManiaginabilybyByByByByByExternalDependencEctenceScoupling.itallowsforloosecoupling,EasiererTestingThroughMocking,andModularDesign,andModularDesign,butquirscarecarefulscarefullsstructoringDovairing voavoidOverOver-Inje

PHP性能优化可以通过以下步骤实现:1)在脚本顶部使用require_once或include_once减少文件加载次数;2)使用预处理语句和批处理减少数据库查询次数;3)配置OPcache进行opcode缓存;4)启用并配置PHP-FPM优化进程管理;5)使用CDN分发静态资源;6)使用Xdebug或Blackfire进行代码性能分析;7)选择高效的数据结构如数组;8)编写模块化代码以优化执行。

opcodecachingsimplovesphperforvesphpermance bycachingCompiledCode,reducingServerLoadAndResponSetimes.1)itstorescompiledphpcodeinmemory,bypassingparsingparsingparsingandcompiling.2)useopcachebachebachebachebachebachebachebysettingparametersinphametersinphp.ini,likeememeryconmorysmorysmeryplement.33)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

Dreamweaver Mac版
视觉化网页开发工具

WebStorm Mac版
好用的JavaScript开发工具