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
// 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 instructionsecho "<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
-
Store an action value:
- URL:
yourscript.php?action=your_value
- Example:
yourscript.php?action=open_door
- URL:
-
Check stored value:
- URL:
yourscript.php?cmd=status
- Will display the currently stored action
- URL:
-
Delete stored value:
- URL:
yourscript.php?cmd=delete
- Clears the stored action
- URL:
Key Features
- Uses URL parameters for all operations (
action
,cmd
) - Session-based storage persists between requests
- Full English interface and comments
- Input sanitization with
htmlspecialchars()
- Case-insensitive command handling
- Clear usage instructions
- 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