PHP头条
热点:

[原创][技术]PHP学习笔记(5)--PHP高级2/2,学习笔记--php


主要内容:

PHP E-mail
PHP 安全 E-mail
PHP Error
PHP Exception
PHP Filter

正文:

-------------------------------------------------------------------

PHP E-mail

 <?php

$to = "someone@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent.";

?>

看上去似乎不能运行,显示Failed to connect to mailserver at "localhost" port 25,

-------------------------------------------------------------------

PHP 安全 E-mail

 

-------------------------------------------------------------------

PHP Error

1) die()

<?php
if(!file_exists("welcome.txt"))
 {
 die("似乎不太对,但是我不会告诉你具体出了什么问题");
 }
else
 {
 $file=fopen("welcome.txt","r");
 }
?>
 这样,本来应该显示“failed to open stream: No such file or directory ”的情况,变成了出现我们想要的提示。

2) 自定义错误和错误触发器

 

3) 错误报告

 

 

-------------------------------------------------------------------

PHP Exception

和一些面向对象的编程语言一样,有try ...throw...catch 可以用来处理异常(Exception)

<?php
//创建可抛出一个异常的函数
function checkNum($number)
 {
 if($number>1)
  {
  throw new Exception("Value must be 1 or below");
  }
 return true;
 }

//在 "try" 代码块中触发异常
try
 {
 checkNum(2);
 //If the exception is thrown, this text will not be shown
 echo 'If you see this, the number is 1 or below';
 }

//捕获异常
catch(Exception $e)
 {
 echo 'Message: ' .$e->getMessage();
 }
?>
运行结果是:

Message: Value must be 1 or below

异常的规则
需要进行异常处理的代码应该放入 try 代码块内,以便捕获潜在的异常。
每个 try 或 throw 代码块必须至少拥有一个对应的 catch 代码块。
使用多个 catch 代码块可以捕获不同种类的异常。
可以在 try 代码块内的 catch 代码块中再次抛出(re-thrown)异常。
简而言之:如果抛出了异常,就必须捕获它

-------------------------------------------------------------------

PHP Filter

什么是 PHP 过滤器?
PHP 过滤器用于验证和过滤来自非安全来源的数据。

验证和过滤用户输入或自定义数据是任何 Web 应用程序的重要组成部分。

设计 PHP 的过滤器扩展的目的是使数据过滤更轻松快捷。
为什么使用过滤器?
几乎所有 web 应用程序都依赖外部的输入。这些数据通常来自用户或其他应用程序(比如 web 服务)。通过使用过滤器,您能够确保应有程序获得正确的输入类型。

您应该始终对外部数据进行过滤!

输入过滤是最重要的应用程序安全课题之一。

什么是外部数据?
来自表单的输入数据
Cookies
服务器变量
数据库查询结果

 

www.phpzy.comtrue/php/8151.htmlTechArticle[原创][技术]PHP学习笔记(5)--PHP高级2/2,学习笔记--php 主要内容: PHP E-mail PHP 安全 E-mail PHP Error PHP Exception PHP Filter 正文: ------------------------------------------------------------------- PHP E-mail  ...

相关文章

    暂无相关文章
相关频道:

PHP之友评论

今天推荐