fork download
  1. <?php
  2. // register_demo_w3.php
  3. // Single-file demo suitable for W3Schools PHP compiler.
  4. // Intent: Demonstrate secure registration flow (CSRF, validation, password_hash) in an environment
  5. // where persistent DB is unavailable. Data is kept in session only for demonstration.
  6.  
  7.  
  8. // CSRF token for the session
  9. if (empty($_SESSION['csrf_token'])) {
  10. $_SESSION['csrf_token'] = bin2hex(random_bytes(16));
  11. }
  12. $csrf = $_SESSION['csrf_token'];
  13.  
  14. $errors = [];
  15. $success = null;
  16.  
  17. // Handle POST submission in same file
  18. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  19. $posted_csrf = $_POST['csrf'] ?? '';
  20. if (empty($posted_csrf) || !hash_equals($csrf, $posted_csrf)) {
  21. $errors[] = 'Invalid CSRF token. Reload and try again.';
  22. } else {
  23. // Normalize inputs
  24. $username = trim((string)($_POST['username'] ?? ''));
  25. $email = trim((string)($_POST['email'] ?? ''));
  26. $password = (string)($_POST['password'] ?? '');
  27. $confirm = (string)($_POST['confirm_password'] ?? '');
  28.  
  29. if ($username === '') $errors[] = 'Username is required.';
  30. if ($email === '') $errors[] = 'Email is required.';
  31. if ($password === '') $errors[] = 'Password is required.';
  32. if ($confirm === '') $errors[] = 'Please confirm password.';
  33.  
  34. if ($email !== '' && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
  35. $errors[] = 'Invalid email format.';
  36. }
  37.  
  38. if (mb_strlen($username) < 3 || mb_strlen($username) > 50) {
  39. $errors[] = 'Username must be 3–50 characters.';
  40. }
  41.  
  42. // Password policy
  43. $pw_ok = true;
  44. if (strlen($password) < 8) $pw_ok = false;
  45. if (!preg_match('/[A-Z]/', $password)) $pw_ok = false;
  46. if (!preg_match('/[a-z]/', $password)) $pw_ok = false;
  47. if (!preg_match('/[0-9]/', $password)) $pw_ok = false;
  48. if (!preg_match('/[\W_]/', $password)) $pw_ok = false;
  49. if (!$pw_ok) $errors[] = 'Password must be 8+ chars and include upper, lower, number and symbol.';
  50.  
  51. if ($password !== $confirm) $errors[] = 'Passwords do not match.';
  52.  
  53. // If valid, "save" into session demo storage
  54. if (empty($errors)) {
  55. if (!isset($_SESSION['demo_users'])) $_SESSION['demo_users'] = [];
  56.  
  57. $email_norm = strtolower($email);
  58. foreach ($_SESSION['demo_users'] as $u) {
  59. if ($u['email'] === $email_norm) {
  60. $errors[] = 'An account with this email already exists (demo).';
  61. break;
  62. }
  63. }
  64. if (empty($errors)) {
  65. $hash = password_hash($password, PASSWORD_DEFAULT);
  66. $_SESSION['demo_users'][] = [
  67. 'id' => uniqid('u', true),
  68. 'username' => $username,
  69. 'email' => $email_norm,
  70. 'password_hash' => $hash,
  71. 'created_at' => date(DATE_ATOM)
  72. ];
  73. $success = 'Registration successful (demo users saved to session).';
  74. // rotate token to reduce replay risk
  75. $_SESSION['csrf_token'] = bin2hex(random_bytes(16));
  76. $csrf = $_SESSION['csrf_token'];
  77. // clear posted values for sticky UI
  78. $_POST = [];
  79. }
  80. }
  81. }
  82. }
  83.  
  84. // Helper
  85. function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); }
  86. ?>
  87. <!doctype html>
  88. <html lang="en">
  89. <head><meta charset="utf-8"><title>Register Demo</title></head>
  90. <body>
  91. <h1>Register (W3Schools Demo)</h1>
  92.  
  93. <?php if (!empty($errors)): ?>
  94. <div style="color:darkred;"><ul><?php foreach ($errors as $e): ?><li><?php echo h($e); ?></li><?php endforeach; ?></ul></div>
  95. <?php endif; ?>
  96.  
  97. <?php if ($success): ?>
  98. <div style="color:green"><?php echo h($success); ?></div>
  99. <?php endif; ?>
  100.  
  101. <form method="post" action="">
  102. <input type="hidden" name="csrf" value="<?php echo h($csrf); ?>">
  103. <label>Username:<br><input type="text" name="username" required maxlength="50" value="<?php echo isset($_POST['username'])?h($_POST['username']):''; ?>"></label><br><br>
  104. <label>Email:<br><input type="email" name="email" required value="<?php echo isset($_POST['email'])?h($_POST['email']):''; ?>"></label><br><br>
  105. <label>Password:<br><input type="password" name="password" required></label><br><br>
  106. <label>Confirm Password:<br><input type="password" name="confirm_password" required></label><br><br>
  107. <button type="submit">Register</button>
  108. </form>
  109.  
  110. <hr>
  111. <h2>Demo: Registered users (session)</h2>
  112. <?php if (!empty($_SESSION['demo_users'])): ?>
  113. <ul>
  114. <?php foreach ($_SESSION['demo_users'] as $u): ?>
  115. <li><?php echo h($u['username']); ?><?php echo h($u['email']); ?><?php echo h($u['created_at']); ?></li>
  116. <?php endforeach; ?>
  117. </ul>
  118. <?php else: ?>
  119. <p>No demo users yet.</p>
  120. <?php endif; ?>
  121.  
  122. <p><small>Note: This demo stores users in session only. Use a database and stronger infrastructure for production.</small></p>
  123. </body>
  124. </html>
  125.  
Success #stdin #stdout #stderr 0.03s 26208KB
stdin
Standard input is empty
stdout
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Register Demo</title></head>
<body>
  <h1>Register (W3Schools Demo)</h1>

  
  
  <form method="post" action="">
    <input type="hidden" name="csrf" value="dc17516decdcee5b221bbbfca1cd80ea">
    <label>Username:<br><input type="text" name="username" required maxlength="50" value=""></label><br><br>
    <label>Email:<br><input type="email" name="email" required value=""></label><br><br>
    <label>Password:<br><input type="password" name="password" required></label><br><br>
    <label>Confirm Password:<br><input type="password" name="confirm_password" required></label><br><br>
    <button type="submit">Register</button>
  </form>

  <hr>
  <h2>Demo: Registered users (session)</h2>
      <p>No demo users yet.</p>
  
  <p><small>Note: This demo stores users in session only. Use a database and stronger infrastructure for production.</small></p>
</body>
</html>
stderr
PHP Notice:  Undefined index: REQUEST_METHOD in /home/Ssc5S6/prog.php on line 19