Repository: phprouter/main Branch: main Commit: 44481ae28d63 Files: 10 Total size: 7.2 KB Directory structure: gitextract_s62tl7hk/ ├── .htaccess ├── LICENSE ├── README.md ├── api/ │ └── save_user.php ├── router.php ├── routes.php └── views/ ├── 404.php ├── full_name.php ├── index.php └── user.php ================================================ FILE CONTENTS ================================================ ================================================ FILE: .htaccess ================================================ RewriteEngine On RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.webp|\.gif|\.jpeg|\.zip|\.css|\.svg|\.js|\.pdf)$ RewriteRule (.*) routes.php [QSA,L] ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2021 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # PHP ROUTER Secure router with XSS and CSRF 1. Download the file ".htaccess" and place it under the root directory (html, htdocs, or www) of your web server 2. Download the file "router.php" and place it under the root directory (html, htdocs, or www) of your web server 3. Download the file "routes.php" and place it under the root directory (html, htdocs, or www) of your web server In the browser go to "localhost" or "127.0.0.1" and you should see the word "Index" displayed in the website. Feel free to delete all the routes in the "routes.php" file and create your own. Most likely you want to keep the last route for "Page not found". For details about routing, visit https://phprouter.com ================================================ FILE: api/save_user.php ================================================ '; } function is_csrf_valid() { session_start(); if (!isset($_SESSION['csrf']) || !isset($_POST['csrf'])) { return false; } if ($_SESSION['csrf'] != $_POST['csrf']) { return false; } return true; } ================================================ FILE: routes.php ================================================ http://localhost // The output -> Index get('/', 'views/index.php'); // Dynamic GET. Example with 1 variable // The $id will be available in user.php get('/user/$id', 'views/user'); // Dynamic GET. Example with 2 variables // The $name will be available in full_name.php // The $last_name will be available in full_name.php // In the browser point to: localhost/user/X/Y get('/user/$name/$last_name', 'views/full_name.php'); // Dynamic GET. Example with 2 variables with static // In the URL -> http://localhost/product/shoes/color/blue // The $type will be available in product.php // The $color will be available in product.php get('/product/$type/color/$color', 'product.php'); // A route with a callback get('/callback', function(){ echo 'Callback executed'; }); // A route with a callback passing a variable // To run this route, in the browser type: // http://localhost/user/A get('/callback/$name', function($name){ echo "Callback executed. The name is $name"; }); // Route where the query string happends right after a forward slash get('/product', ''); // A route with a callback passing 2 variables // To run this route, in the browser type: // http://localhost/callback/A/B get('/callback/$name/$last_name', function($name, $last_name){ echo "Callback executed. The full name is $name $last_name"; }); // ################################################## // ################################################## // ################################################## // Route that will use POST data post('/user', '/api/save_user'); // ################################################## // ################################################## // ################################################## // any can be used for GETs or POSTs // For GET or POST // The 404.php which is inside the views folder will be called // The 404.php has access to $_GET and $_POST any('/404','views/404.php'); ================================================ FILE: views/404.php ================================================