PHP拾遗(1)
自从因为工作需要转向C#.NET之后, PHP就被放在了一边,都快两年了.前一段尝试用PHP开发了闪图闪字网,发现自己的PHP水平已经远远的跟不上PHP的发展速度了,想想我最爱的PHP,决定重新拾起来,再好好的学习学习.
记录一些零星的代码,只为了自己备忘.
1. 关于magic quote, magic quote可以在php的配置文件中开启/关闭. magic quote开启后会自动转义输入的数据, 好处有两点, 第一是在进行数据库操作的时候自动转义. 第二对于初学者来说在一定程度上提高了安全性, 最明显的例子就是在一定程度上避免了SQL注入的安全隐患.
但是magic quote带来的问题也是很明显的, 第一是代码的可移植性,需要后来者不停的进行数据后续操作. 其次所有的数据都被转义的话就会带来性能问题,况且很多情况下我们并不需要进行转义. 所以magic quote在PHP6中已经被废弃.
对于代码移植来说, 下面的代码可以帮助你, 这是从网上查到的:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php if (get_magic_quotes_gpc()) { function stripslashes_deep($value) { $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value); return $value; } $_GET = array_map('stripslashes_deep', $_GET); $_POST = array_map('stripslashes_deep', $_POST); $_COOKIE = array_map('stripslashes_deep', $_COOKIE); $_REQUEST = array_map('stripslashes_deep', $_REQUEST); } ?> |
2. 如何安全的执行一条SQL语句
这里还是提醒初学者朋友们, 什么是经典, 还是PHP手册, 以下代码摘自PHP手册:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <?php if (isset($_POST['product_name']) && isset($_POST['product_description']) && isset($_POST['user_id'])) { // Connect $link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password'); if(!is_resource($link)) { echo "Failed to connect to the server\n"; // ... log the error properly } else { // Reverse magic_quotes_gpc/magic_quotes_sybase effects on those vars if ON. if(get_magic_quotes_gpc()) { $product_name = stripslashes($_POST['product_name']); $product_description = stripslashes($_POST['product_description']); } else { $product_name = $_POST['product_name']; $product_description = $_POST['product_description']; } // Make a safe query $query = sprintf("INSERT INTO products (`name`, `description`, `user_id`) VALUES ('%s', '%s', %d)", mysql_real_escape_string($product_name, $link), mysql_real_escape_string($product_description, $link), $_POST['user_id']); mysql_query($query, $link); if (mysql_affected_rows($link--> 0) { echo "Product inserted\n"; } } } else { echo "Fill the form properly\n"; } ?> |
3. 使用公共方法
内部实现使用名称的关联数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <?php class Person { private $personName = array(); public function setPrefix($prefix) { $this->personName['prefix'] = $prefix; } public function getPrefix() { return $this->personName['prefix']; } public function setGivenName($gn) { $this->personName['givenName'] = $gn; } public function getGivenName() { return $this->personName['givenName']; } /* etc... */ } /* * Even though the internal implementation changed, the code here stays exactly * the same. The change has been encapsulated only to the Person class. */ $person = new Person(); $person->setPrefix("Mr."); $person->setGivenName("John"); echo($person->getPrefix()); echo($person->getGivenName()); ?> |
4. 抛出错误
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php class InvalidPersonNameFormatException extends LogicException {} class PersonUtils { public static function parsePersonName($format, $val) { if (! $format) { throw new InvalidPersonNameFormatException("Invalid PersonName format."); } if ((! isset($val)) || strlen($val) == 0) { throw new InvalidArgumentException("Must supply a non-null value to parse."); } } } ?> |
Recent Comments