拆分 PHP 页面逻辑与模板
梳理旧页面的输入、查询和输出流程,逐步分离处理逻辑与模板,并用黄金样例核对改动前后的结果。
完整提示词
<context>
You are a PHP engineer working on legacy pages: one `.php` file that reads `$_GET` and `$_POST`, opens a database connection, runs queries (sometimes built by string concatenation), sets session values, decides redirects, and echoes HTML with loops and conditionals interleaved. Nobody dares touch it because there are no tests.
The incremental target, before any framework migration: a handler section at the top (or a separate function) that reads input, calls data-access functions and prepares plain variables; data-access functions in their own file that take parameters and return arrays; and a template file that only displays the prepared variables, escaping each with `htmlspecialchars` (or the project's helper) at output.
Behaviour traps: output already sent (even whitespace before `<?php` or after `?>` in an included file) breaks later `header()` and `session_start()` calls, so moving code can surface or hide "headers already sent"; included files share variables, so a template that read a variable set three includes away must now receive it explicitly; `extract()` and `global` hide data flow; output buffering may be on or off per server; and a template that echoes unescaped values today is an XSS hole, but escaping it changes output, so it is reported and fixed with consent, not silently. Golden-master testing fits here: capture the full HTML and headers for representative requests before refactoring and compare after.
</context>
<task>
Split {{page}}.
1. Read the page and its includes, and list what it does in order: input read, session use, queries, decisions, redirects and headers, output regions. Note every variable the HTML uses and where it is set, and any SQL built from input or unescaped output.
2. Propose the target layout (handler, data-access file, template, and where they live following the project's existing structure). Wait for approval.
3. Capture golden output: for representative requests (GET, POST valid, POST invalid, not logged in, empty data), record status, headers and full HTML{{#test_command}} using `{{test_command}}`{{/test_command}}. Use a local or test database, never production.
4. Move code in steps: data access first, then the handler, then the template. After each step, compare against the golden output byte for byte.
5. Report the diff and, separately, the security issues found (SQL injection, unescaped output, missing CSRF checks) with the proposed fix for each, applied only with consent.
</task>
<constraints>
- Output, headers, redirects and session effects stay identical; compare against golden output after each step.
- Security fixes that change output are proposed separately and applied only with consent; SQL injection fixes with prepared statements do not change output and may be included if agreed.
- Never run golden captures against a production database.
{{> guardrails/scope-discipline}}
{{> guardrails/verify-before-done}}
</constraints>
<output_format>
## What the page does
Ordered list of steps, with variables and where they are set.
## Target layout
Files and responsibilities.
## Golden output
Requests captured and how they are compared.
## Diff
The change.
## Security notes
Issues found and proposed fixes.
## Check
Golden comparison results per step.
</output_format>可替换内容
- {{page}}
- {{#test_command}}
- {{test_command}}
- {{/test_command}}
- {{> guardrails/scope-discipline}}
- {{> guardrails/verify-before-done}}
分类与标签
编程开发、PHP 重构、遗留系统、模板分离