3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Challenge: make this terrible code safe if((count($_POST) > 0) && (strlen($_POST['password']) == 0 || strlen($_POST['username']) == 0)){ echo "Please enter username and password".PHP_EOL; exit(); } try{ $pdo = new PDO('sqlite::memorytest:'); } catch(PDOException $e){ echo $e->getMessage(); } $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT); $pdo->exec("DROP TABLE IF EXISTS users"); $pdo->exec("CREATE TABLE users (username VARCHAR(255), password VARCHAR(255))"); $rootPassword = password_hash("secret", PASSWORD_DEFAULT); $pdo->exec("INSERT INTO users (username, password) VALUES ('root', '$rootPassword');"); $statement = $pdo->prepare("SELECT password FROM users WHERE username = ? LIMIT 1"); if($statement->execute(array($_POST['username']))){ $row = $statement->fetchAll(); if(password_verify($_POST['password'], $row[0]['password'])){ echo "Access granted to $username!<br />\n"; exit(); } else{ echo "Access denied for $username!<br>\n"; } } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Challenge 4</title> </head> <body> <form method="post"> <input type="text" name="username" placeholder="username" /> <input type="password" name="password" placeholder="password" /> <input type="submit" value="Submit" /> </form> </body> </html>
Output for 7.0.5 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.26, 7.3.0 - 7.3.13, 7.4.0 - 7.4.1
open_basedir prohibits opening :memorytest: Notice: Undefined variable: pdo in /in/1aGo0 on line 16 Fatal error: Uncaught Error: Call to a member function setAttribute() on null in /in/1aGo0:16 Stack trace: #0 {main} thrown in /in/1aGo0 on line 16
Process exited with code 255.
Output for 7.0.0 - 7.0.4
open_basedir prohibits opening :memorytest: Notice: Undefined variable: pdo in /in/1aGo0 on line 16 Fatal error: Uncaught Error: Call to a member function setAttribute() on unknown in /in/1aGo0:16 Stack trace: #0 {main} thrown in /in/1aGo0 on line 16
Process exited with code 255.
Output for 5.6.0 - 5.6.40
Warning: PDO::__construct(): open_basedir restriction in effect. File(/:memorytest:) is not within the allowed path(s): (/tmp:/in:/etc) in /in/1aGo0 on line 11 open_basedir prohibits opening :memorytest: Notice: Undefined variable: pdo in /in/1aGo0 on line 16 Fatal error: Call to a member function setAttribute() on null in /in/1aGo0 on line 16
Process exited with code 255.
Output for 5.5.0 - 5.5.38
Warning: PDO::__construct(): open_basedir restriction in effect. File(/:memorytest:) is not within the allowed path(s): (/tmp:/in:/etc) in /in/1aGo0 on line 11 open_basedir prohibits opening :memorytest: Notice: Undefined variable: pdo in /in/1aGo0 on line 16 Fatal error: Call to a member function setAttribute() on a non-object in /in/1aGo0 on line 16
Process exited with code 255.
Output for 5.1.0 - 5.1.3, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45
SQLSTATE[HY000] [14] unable to open database file Notice: Undefined variable: pdo in /in/1aGo0 on line 16 Fatal error: Call to a member function setAttribute() on a non-object in /in/1aGo0 on line 16
Process exited with code 255.
Output for 5.1.4 - 5.1.6
could not find driver Notice: Undefined variable: pdo in /in/1aGo0 on line 16 Fatal error: Call to a member function setAttribute() on a non-object in /in/1aGo0 on line 16
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Fatal error: Class 'PDO' not found in /in/1aGo0 on line 11
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected '{' in /in/1aGo0 on line 10
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected '{' in /in/1aGo0 on line 10
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/1aGo0 on line 10
Process exited with code 255.

preferences:
224.2 ms | 401 KiB | 325 Q