PHP 5.5 Action Management with Parameters (English Version)

PHP 5.5 Action Management with Parameters (English Version)

Here's a PHP 5.5 compatible script that uses URL parameters instead of paths for all operations:

php 复制代码
<?php
// Start session for persistent storage
session_start();

// Initialize the stored action if not set
if (!isset($_SESSION['stored_action'])) {
    $_SESSION['stored_action'] = null;
}

// Process requests based on parameters
if (isset($_GET['action'])) {
    // Store the action value
    $_SESSION['stored_action'] = $_GET['action'];
    echo "Action stored: " . htmlspecialchars($_GET['action']);
} elseif (isset($_GET['cmd'])) {
    $command = strtolower($_GET['cmd']);
    
    if ($command === 'status') {
        // Return the stored action value
        $action = $_SESSION['stored_action'];
        echo "Current action: " . ($action ? htmlspecialchars($action) : "No action stored");
    } elseif ($command === 'delete') {
        // Delete the stored action
        $_SESSION['stored_action'] = null;
        echo "Action deleted successfully";
    } else {
        echo "Invalid command";
    }
} else {
    // Display usage instructions
    echo "<h1>Action Management System</h1>";
    echo "<p>Usage:</p>";
    echo "<ul>";
    echo "<li>Add <code>?action=value</code> to store a value</li>";
    echo "<li>Add <code>?cmd=status</code> to view stored value</li>";
    echo "<li>Add <code>?cmd=delete</code> to delete stored value</li>";
    echo "</ul>";
    echo "<p>Examples:</p>";
    echo "<ul>";
    echo "<li><a href='?action=test_value'>?action=test_value</a></li>";
    echo "<li><a href='?cmd=status'>?cmd=status</a></li>";
    echo "<li><a href='?cmd=delete'>?cmd=delete</a></li>";
    echo "</ul>";
}
?>

How to Use

  1. Store an action value:

    • URL: yourscript.php?action=your_value
    • Example: yourscript.php?action=open_door
  2. Check stored value:

    • URL: yourscript.php?cmd=status
    • Will display the currently stored action
  3. Delete stored value:

    • URL: yourscript.php?cmd=delete
    • Clears the stored action

Key Features

  1. Uses URL parameters for all operations (action, cmd)
  2. Session-based storage persists between requests
  3. Full English interface and comments
  4. Input sanitization with htmlspecialchars()
  5. Case-insensitive command handling
  6. Clear usage instructions
  7. PHP 5.5 compatible

Security Notes

  • Always sanitize output with htmlspecialchars()
  • Session storage is more secure than using a global variable
  • Consider adding CSRF protection for production use
  • For sensitive data, consider database storage instead of sessions
相关推荐
杨DaB1 小时前
【SpringMVC】拦截器,实现小型登录验证
java·开发语言·后端·servlet·mvc
近津薪荼1 小时前
c++详解(宏与内联函数,nullptr)
开发语言·c++
Monkey-旭6 小时前
Android Bitmap 完全指南:从基础到高级优化
android·java·人工智能·计算机视觉·kotlin·位图·bitmap
天若有情6737 小时前
【python】Python爬虫入门教程:使用requests库
开发语言·爬虫·python·网络爬虫·request
寒水馨7 小时前
Java 17 新特性解析与代码示例
java·开发语言·jdk17·新特性·java17
启山智软7 小时前
选用Java开发商城的优势
java·开发语言
秃然想通7 小时前
掌握Python三大语句:顺序、条件与循环
开发语言·python·numpy
##学无止境##8 小时前
Maven 从入门到精通:Java 项目构建与依赖管理全解析(上)
java·开发语言·maven
魔都吴所谓8 小时前
【go】语言的匿名变量如何定义与使用
开发语言·后端·golang