由于公司需要php方面的项目开发,php刚刚入门,在写按钮提交过程中,asp.net里的按钮事件更好些。先看下面的代码,
<? require_once '../inc/EventHelper.php'; function Page_Load() { echo '在任何时候都会运行<br>'; if(!Page::IsPostBack()) { echo '加载产品分类<br>'; if($_GET['cmd']=='edit') { echo '修改加载需要修改的产品信息<br>'; } } } function bAdd_Click() { //Comm::CheckQX('产品管理_添加'); echo "bAdd_Click<br>"; } function bEdit_Click() { //Comm::CheckQX('产品管理_修改'); echo 'proID='.$_GET['proID'].'<br>'; echo "bEdit_Click<br>"; } function sdfsdfdsf_Click() { echo "e44444444444444444444<br>"; } ?>
<form name="aa" method="post" action="?<?=Comm::GetParam()?>"> <input type="submit" name="bAdd" value="添加" /> <input type="submit" name="bedit" value="修改" /> <input type="submit" name="sdfsdfdsf" value="ewrewrewr" /> </form>
做过asp.net开发的对上面的代码应该有点熟悉,如:Page_Load,Page.IsPostback,bAdd_Click,这些很像asp.net的事件。
以上代码在点击【添加】按钮时运行bAdd_Click函数(暂且这么叫,function好像是函数的意思),同样的,点击【修改】按钮自动运行bEdit_Click事件。不需要过多的参数变化,也不需要过多的文件,若页面功能不是很复杂的情况下,用这种模式可以快速开发。
下面来看看EventHelper.php这个文件的代码:
<? class Page { //是否回发数据,1:是 public static function IsPostBack() { global $SYSRunEventName; return !empty($SYSRunEventName); } //加载并执行事件 function EventLoad() { global $SYSRunEventName; $arrEvent=get_defined_functions(); $arrEventUser=$arrEvent['user']; $arr=array_keys($_POST); foreach($arr as $row) { $name=strtolower($row); foreach($arrEventUser as $row1) { $name1=str_ireplace('_click','',$row1); if($name==$name1) { $SYSRunEventName=$row1; break; } } if(!empty($SYSRunEventName)) { break; } } if(function_exists('Page_Load')) Page_Load(); $SYSRunEventRunName=strtolower($SYSRunEventName); if(Page::IsPostBack()) { $SYSRunEventName(); } } } class Comm { public static function GetParam($params=array(),$cmd='addoverride') { $allParam=array(); if($cmd=='addoverride') { $arrKeys=array_keys($params); foreach($arrKeys as $row) { if(!in_array($row,array_keys($allParam))) $allParam[$row]=$params[$row]; } } else if($cmd=='del') { foreach($params as $row) { unset($_GET[$row]); } } $arrKeys=array_keys($_GET); foreach($arrKeys as $row) { if(!in_array($row,array_keys($allParam))) $allParam[$row]=$_GET[$row]; } $p=''; $arrKeys=array_keys($allParam); foreach($arrKeys as $row) { $p.=$row.'='.$allParam[$row].'&'; } return rtrim($p,'&'); } } Page::EventLoad(); ?>
以上功能大家可以测试下,在我php5.4可以运行成功,不过在安全性方面还没考虑过多,看过一些文章php会有可能通过客户端执行php代码,因为php很许多实用的功能特性。
关于Comm::GetParam,由于经常需要获取get方式的参数,或修改参数,如分页时需要保留所有url参数,只修改分页参数(如page=5),所以就自行写了一些代码。
主要利用php以下几个特点:
function_exists
get_defined_functions
以及利用常用的表单提交原理,利用submit提交原理 实现功能的。
由于时间仓促,来不及说明具体原理,还请见谅,代码大家都可以看懂的。

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.

The article discusses various methods for page redirection in PHP, focusing on the header() function and addressing common issues like "headers already sent" errors.

Article discusses type hinting in PHP, a feature for specifying expected data types in functions. Main issue is improving code quality and readability through type enforcement.

The article discusses PHP Data Objects (PDO), an extension for database access in PHP. It highlights PDO's role in enhancing security through prepared statements and its benefits over MySQLi, including database abstraction and better error handling.

Article discusses creating and securing PHP APIs, detailing steps from endpoint definition to performance optimization using frameworks like Laravel and best security practices.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version
Recommended: Win version, supports code prompts!
