| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- session_start();
- if (isset($_SESSION['user_id'])) {
- header('Location: index.php');
- exit;
- }
- $pdo = new PDO('mysql:host=sql101.infinityfree.com;dbname=if0_39567875_nex', 'if0_39567875', 'PIvOR9WViHm'); // Update credentials
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- $username = $_POST['username'];
- $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
- if (isset($_POST['register'])) {
- // Register
- $stmt = $pdo->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
- if ($stmt->execute([$username, $password])) {
- $_SESSION['user_id'] = $pdo->lastInsertId();
- header('Location: index.php');
- } else {
- echo "Username taken.";
- }
- } elseif (isset($_POST['login'])) {
- // Login
- $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
- $stmt->execute([$username]);
- $user = $stmt->fetch();
- if ($user && password_verify($_POST['password'], $user['password'])) {
- $_SESSION['user_id'] = $user['id'];
- header('Location: index.php');
- } else {
- echo "Invalid credentials.";
- }
- }
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Register/Login</title>
- <link rel="stylesheet" href="styles.css">
- </head>
- <body>
- <div class="container">
- <h2>Register</h2>
- <form method="POST">
- <input type="text" name="username" placeholder="Username" required>
- <input type="password" name="password" placeholder="Password" required>
- <button type="submit" name="register">Register</button>
- </form>
- <h2>Login</h2>
- <form method="POST">
- <input type="text" name="username" placeholder="Username" required>
- <input type="password" name="password" placeholder="Password" required>
- <button type="submit" name="login">Login</button>
- </form>
- </div>
- </body>
- </html>
|