3v4l.org

run code in 300+ PHP versions simultaneously
<?php require 'Slim/Slim.php'; require 'Slim/Middleware.php'; require 'connect.php'; require 'error_codes.php'; Slim::registerAutoloader(); date_default_timezone_set('Europe/Warsaw'); $app = new Slim(); $app->response()->header('Content-Type', 'application/json'); $app->add(new Slim_Middleware_SessionCookie(array('secret' => 'mp1024sec', 'expires' => '60 minutes', 'httponly' => true))); $db = null; $user = null; // LOGIN $authenticate = function ($app) { return function () use ($app) { global $user, $db; if (!isset($_SESSION['user'])) { $app->response->setStatus(401); } else { $db = getConnection(); $sql = 'select * from users where email=:email'; $stmt = $db->prepare($sql); $stmt->bindValue(':email', $_SESSION['user'], PDO::PARAM_STR); $stmt->execute(); $results = $stmt->fetchAll(PDO::FETCH_ASSOC); if(count($results) != 1) { $app->response->setStatus(401); } else { $user = $results[0]; unset($user['password']); } } }; }; $app->post('/register', 'register'); function register() { try { global $db, $app; $obj = json_decode($app->request->getBody()); if($obj->name != null && $obj->surname != null && $obj->email != null && filter_var($obj->email, FILTER_VALIDATE_EMAIL) && $obj->password != null) { // TODO check if user already exists in users and users_requests $sql = 'insert into users_requests(name, surname, email, password, expires, token) values(:name, :surname, :email, :password, :expires, :token)'; $db = getConnection(); $stmt = $db->prepare($sql); $stmt->bindValue(':name', $obj->name, PDO::PARAM_STR); $stmt->bindValue(':surname', $obj->surname, PDO::PARAM_STR); $stmt->bindValue(':email', $obj->email, PDO::PARAM_STR); $stmt->bindValue(':password', password_hash($obj->password, PASSWORD_DEFAULT), PDO::PARAM_STR); $stmt->bindValue(':expires', date('Y-m-d H:i:s', strtotime('+2 days'))); $token = md5(uniqid(mt_rand(), true)); $stmt->bindValue(':token', $token, PDO::PARAM_STR); $stmt->execute(); $db = null; // TODO send email to ADMIN echo json_encode(array('status' => 'success')); } else { paramError($app, 'user'); } } catch(PDOException $e) { pdoError($e, $app); } } $app->get('/login', 'getLogin'); function getLogin() { try { global $db, $app; if(isset($_SESSION['user'])) { $db = getConnection(); $sql = 'select * from users where email=:email'; $stmt = $db->prepare($sql); $stmt->bindValue(':email', $_SESSION['user'], PDO::PARAM_STR); $stmt->execute(); $results = $stmt->fetchAll(PDO::FETCH_ASSOC); if(count($results) != 1) { $app->response->setStatus(401); unauthorizedError(); return; } else { $user = $results[0]; unset($user['password']); unset($user['id']); echo json_encode(array('status' => 'success', 'user' => $user)); } } else { $app->response()->status(401); unauthorizedError(); } $db = null; } catch(PDOException $e) { pdoError($e, $app); } } $app->post('/login', 'postLogin'); function postLogin() { try { global $db, $app; $obj = json_decode($app->request()->getBody()); if($obj == null) { codeError($app, LOGIN_MISSING_USER); return; } else if($obj->email == null) { codeError($app, LOGIN_MISSING_EMAIL); return; } else if($obj->password == null) { codeError($app, LOGIN_MISSING_PASSWORD); return; } $sql = 'select * from users where email=:email'; $db = getConnection(); $stmt = $db->prepare($sql); $stmt->bindValue(':email', $obj->email, PDO::PARAM_STR); $stmt->execute(); $results = $stmt->fetchAll(PDO::FETCH_ASSOC); if(count($results) != 1) { codeError($app, LOGIN_USER_NOT_FOUND); return; } $user = $results[0]; if(!password_verify($obj->password, $user['password'])) { codeError($app, LOGIN_WRONG_PASSWORD); return; } $_SESSION['user'] = $user['email']; unset($user['password']); unset($user['id']); $db = null; echo json_encode(array('status' => 'success', 'user' => $user)); } catch(PDOException $e) { pdoError($e, $app); } } $app->post('/logout', 'postLogout'); function postLogout() { unset($_SESSION['user']); echo json_encode(array('status' => 'success')); } // ANKIETY $app->get('/ankiety', $authenticate($app), 'getAnkiety'); function getAnkiety() { global $user, $db, $app; if($app->response()->status() == 401 || $user == null) { unauthorizedError(); return; } try { $sql = ''; $stmt = null; if($user['role'] === 'ADMIN') { $sql = 'select * from ankiety'; $stmt = $db->prepare($sql); } else if($user['role'] === 'USER') { $sql = 'select id, title, message from ankiety where userId=:id'; $stmt = $db->prepare($sql); $stmt->bindValue(':id', $user['id'], PDO::PARAM_INT); } $stmt->execute(); $results = $stmt->fetchAll(PDO::FETCH_ASSOC); $db = null; echo json_encode($results); } catch (PDOException $e) { pdoError($e, $app); } } $app->get('/ankiety/:id', $authenticate($app), 'getAnkietyId'); function getAnkietyId($id) { global $user, $db, $app; if($app->response()->status() == 401 || $user == null) { unauthorizedError(); return; } try { $sql = ''; $stmt = ''; if($user['role'] === 'ADMIN') { $sql = 'select * from ankiety where id=:id'; $stmt = $db->prepare($sql); $stmt->bindValue(':id', $id, PDO::PARAM_INT); } else if($user['role'] === 'USER') { $sql = 'select id, title, message from ankiety where id=:id and userId=:userId'; $stmt = $db->prepare($sql); $stmt->bindValue(':id', $id, PDO::PARAM_INT); $stmt->bindValue(':userId', $user['id'], PDO::PARAM_INT); } $stmt->execute(); $results = $stmt->fetchAll(PDO::FETCH_ASSOC); $db = null; if(count($results) === 1) echo json_encode($results[0]); else echo json_encode(array('status' => 'error', 'error' => 'ankieta not found')); } catch (PDOException $e) { pdoError($e, $app); } } $app->post('/ankiety', $authenticate($app), 'postAnkiety'); function postAnkiety() { global $user, $db, $app; if($app->response()->status() == 401 || $user == null) { unauthorizedError(); return; } try { $obj = json_decode($app->request()->getBody()); if(validateAnkieta($obj)) { $sql = ''; $stmt = ''; $sql = 'insert into ankiety(title, message, userId) values(:title, :message, :userId)'; $stmt = $db->prepare($sql); $stmt->bindValue(':title', $obj->title, PDO::PARAM_STR); $stmt->bindValue(':message', $obj->message, PDO::PARAM_STR); $stmt->bindValue(':userId', $user['id'], PDO::PARAM_INT); $stmt->execute(); $obj->id = $db->lastInsertId(); if($user['role'] === 'ADMIN') $obj->userId = $user['id']; echo json_encode($obj); } else { paramError($app, 'ankieta'); } $db = null; } catch (PDOException $e) { pdoError($e, $app); } } $app->put('/ankiety', $authenticate($app), 'putAnkiety'); function putAnkiety() { global $user, $db, $app; if($app->response()->status() == 401 || $user == null) { unauthorizedError(); return; } try { $obj = json_decode($app->request()->getBody()); if(validateAnkieta($obj)) { $sql = ''; $stmt = ''; if($user['role'] === 'ADMIN') { $sql = 'update ankiety set title=:title, message=:message where id=:id'; $stmt = $db->prepare($sql); $stmt->bindValue(':id', $obj->id, PDO::PARAM_INT); $stmt->bindValue(':title', $obj->title, PDO::PARAM_STR); $stmt->bindValue(':message', $obj->message, PDO::PARAM_STR); } else if($user['role'] === 'USER') { $sql = 'update ankiety set title=:title, message=:message where id=:id and userId=:userId'; $stmt = $db->prepare($sql); $stmt->bindValue(':id', $obj->id, PDO::PARAM_INT); $stmt->bindValue(':userId', $user['id'], PDO::PARAM_INT); $stmt->bindValue(':title', $obj->title, PDO::PARAM_STR); $stmt->bindValue(':message', $obj->message, PDO::PARAM_STR); } $stmt->execute(); $db = null; echo json_encode($obj); } else { paramError($app, 'ankieta'); } } catch (PDOException $e) { pdoError($e, $app); } } $app->delete('/ankiety/:id', $authenticate($app), 'deleteAnkietyId'); function deleteAnkietyId($id) { global $user, $db, $app; if($app->response->getStatus() == 401 || $user == null) { unauthorizedError(); return; } try { $sql = ''; $stmt = ''; if($user['role'] === 'ADMIN') { $sql = 'delete from ankiety where id=:id'; $stmt = $db->prepare($sql); $stmt->bindValue(':id', $id, PDO::PARAM_INT); } else if($user['role'] === 'USER') { $sql = 'delete from ankiety where id=:id and userId=:userId'; $stmt = $db->prepare($sql); $stmt->bindValue(':id', $id, PDO::PARAM_INT); $stmt->bindValue(':userId', $user['id'], PDO::PARAM_INT); } $stmt->execute(); $db = null; echo json_encode(array('status' => 'success')); } catch (PDOException $e) { pdoError($e, $app); } } // POSTY $app->get('/posty', 'getPosty'); function getPosty() { global $app; $limit = $app->request()->get('limit'); if($limit != null) { if(!filter_var($limit, FILTER_VALIDATE_INT, array('min_range' => 0))) { $app->response()->status(400); echo json_encode(array('status' => 'error', 'error' => POSTS_ILLEGAL_LIMIT)); return; } else { if($limit < 0) { $app->response()->status(400); echo json_encode(array('status' => 'error', 'error' => POSTS_ILLEGAL_LIMIT)); return; } } } $db = getConnection(); if(isset($_SESSION['user'])) { // request from panel $user = null; $sql = 'select * from users where email=:email'; $stmt = $db->prepare($sql); $stmt->bindValue(':email', $_SESSION['user'], PDO::PARAM_STR); $stmt->execute(); $results = $stmt->fetchAll(PDO::FETCH_ASSOC); if(count($results) != 1) { $app->response()->status(401); codeError($app, ILLEGAL_STATE); return; } else { $user = $results[0]; unset($user['password']); } if($user['role'] === 'ADMIN') { $sql = 'select p.id, p.message, p.timestamp, p.userId, u.name, u.surname, u.email, u.role from posts p join users u on p.userId = u.id order by p.timestamp desc'; if($limit != null) $sql .= ' limit :limit'; $stmt = $db->prepare($sql); if($limit != null) $stmt->bindValue(':limit', (int) $limit, PDO::PARAM_INT); $stmt->execute(); $results = $stmt->fetchAll(PDO::FETCH_ASSOC); echo json_encode($results); } else if($user['role'] === 'USER') { $sql = 'select p.id, p.message, p.timestamp, u.name, u.surname, u.email from posts p join users u on p.userId = u.id where p.userId=:userId order by p.timestamp desc'; if($limit != null) $sql .= ' limit :limit'; $stmt = $db->prepare($sql); $stmt->bindValue(':userId', $user['id'], PDO::PARAM_INT); if($limit != null) $stmt->bindValue(':limit', (int) $limit, PDO::PARAM_INT); $stmt->execute(); $results = $stmt->fetchAll(PDO::FETCH_ASSOC); echo json_encode($results); } } else { // request from 'outside' $sql = 'select p.id, p.message, p.timestamp, u.name, u.surname, u.email from posts p join users u on p.userId = u.id order by p.timestamp desc'; if($limit != null) $sql .= ' limit :limit'; $stmt = $db->prepare($sql); if($limit != null) $stmt->bindValue(':limit', (int) $limit, PDO::PARAM_INT); $stmt->execute(); $results = $stmt->fetchAll(PDO::FETCH_ASSOC); echo json_encode($results); } $db = null; } $app->post('/posty', $authenticate($app), 'postPosty'); function postPosty() { global $user, $db, $app; if($app->response()->status() == 401 || $user == null) { unauthorizedError(); return; } try { $obj = json_decode($app->request()->getBody()); if(validatePost($obj)) { $sql = ''; $stmt = ''; $sql = 'insert into posts(message, userId) values(:message, :userId)'; $stmt = $db->prepare($sql); $stmt->bindValue(':message', $obj->message, PDO::PARAM_STR); $stmt->bindValue(':userId', $user['id'], PDO::PARAM_INT); $stmt->execute(); $obj->id = $db->lastInsertId(); if($user['role'] === 'ADMIN') $obj->userId = $user['id']; echo json_encode($obj); } else { paramError($app, 'post'); } $db = null; } catch (PDOException $e) { pdoError($e, $app); } } $app->put('/posty', $authenticate($app), 'putPosty'); function putPosty() { global $user, $db, $app; if($app->response()->status() == 401 || $user == null) { unauthorizedError(); return; } try { $obj = json_decode($app->request()->getBody()); if(validatePost($obj)) { $sql = ''; $stmt = ''; if($user['role'] === 'ADMIN') { $sql = 'update posts set message=:message where id=:id'; $stmt = $db->prepare($sql); $stmt->bindValue(':id', $obj->id, PDO::PARAM_INT); $stmt->bindValue(':message', $obj->message, PDO::PARAM_STR); } else if($user['role'] === 'USER') { $sql = 'update posts set message=:message where id=:id and userId=:userId'; $stmt = $db->prepare($sql); $stmt->bindValue(':id', $obj->id, PDO::PARAM_INT); $stmt->bindValue(':userId', $user['id'], PDO::PARAM_INT); $stmt->bindValue(':message', $obj->message, PDO::PARAM_STR); } $stmt->execute(); $db = null; echo json_encode($obj); } else { paramError($app, 'post'); } } catch (PDOException $e) { pdoError($e, $app); } } $app->delete('/posty/:id', $authenticate($app), 'deletePostyId'); function deletePostyId() { global $user, $db, $app; if($app->response->getStatus() == 401 && $user == null) { unauthorizedError(); return; } try { $sql = ''; $stmt = ''; if($user['role'] === 'ADMIN') { $sql = 'delete from posts where id=:id'; $stmt = $db->prepare($sql); $stmt->bindValue(':id', $id, PDO::PARAM_INT); } else if($user['role'] === 'USER') { $sql = 'delete from posts where id=:id and userId=:userId'; $stmt = $db->prepare($sql); $stmt->bindValue(':id', $id, PDO::PARAM_INT); $stmt->bindValue(':userId', $user['id'], PDO::PARAM_INT); } $stmt->execute(); $db = null; echo json_encode(array('status' => 'success')); } catch (PDOException $e) { pdoError($e, $app); } } $app->run(); function pdoError($e, $app) { $response = ['error' => 'DB error: ' . $e->getMessage(), 'status' => 'error']; $app->response()->status(500); echo json_encode($response); } function paramError($app, $param) { $app->response()->status(400); echo json_encode(['error' => 'missing parameter: ' . $param, 'status' => 'error']); } function codeError($app, $code) { $app->response()->status(400); echo json_encode(['error' => $code, 'status' => 'error']); } function unauthorizedError() { echo json_encode(array('error' => 'unauthorized', 'status' => 'error')); } function validateAnkieta($ankieta) { if($ankieta->title != null && $ankieta->message != null) return true; return false; } function validatePost($post) { if($post->message != null) return true; return false; } ?>

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
7.4.10.0060.01315.27
7.4.00.0030.01515.07
7.3.130.0030.01315.17
7.3.120.0080.01015.08
7.3.110.0060.01115.00
7.3.100.0050.01014.96
7.3.90.0060.00614.89
7.3.80.0040.00814.93
7.3.70.0080.00714.96
7.3.60.0040.00815.03
7.3.50.0040.01014.95
7.3.40.0060.00615.01
7.3.30.0030.01114.96
7.3.20.0050.01016.59
7.3.10.0100.00616.51
7.3.00.0070.00816.56
7.2.260.0000.01615.26
7.2.250.0080.01015.10
7.2.240.0080.01015.18
7.2.230.0030.01115.21
7.2.220.0050.00915.06
7.2.210.0050.01115.22
7.2.200.0060.00815.11
7.2.190.0080.00815.09
7.2.180.0060.00915.02
7.2.170.0050.00815.03
7.2.160.0090.00815.17
7.2.150.0090.00616.58
7.2.140.0060.00716.71
7.2.130.0050.01016.75
7.2.120.0050.01116.77
7.2.110.0050.01116.56
7.2.100.0040.01116.90
7.2.90.0050.01216.74
7.2.80.0020.01016.74
7.2.70.0100.00716.60
7.2.60.0090.00616.76
7.2.50.0050.01116.67
7.2.40.0050.01216.75
7.2.30.0050.00916.93
7.2.20.0050.01016.77
7.2.10.0070.00716.67
7.2.00.0060.00816.90
7.1.330.0050.00915.68
7.1.320.0090.00615.68
7.1.310.0050.00815.75
7.1.300.0040.01015.59
7.1.290.0070.00615.67
7.1.280.0050.00715.66
7.1.270.0050.00915.61
7.1.260.0060.00815.66
7.1.250.0050.01115.55
7.1.240.0070.00715.66
7.1.230.0030.01015.55
7.1.220.0130.00315.77
7.1.210.0030.01315.77
7.1.200.0000.01015.64
7.1.190.0030.01015.73
7.1.180.0100.00715.77
7.1.170.0070.01115.62
7.1.160.0040.01015.81
7.1.150.0060.01015.71
7.1.140.0030.00715.70
7.1.130.0060.00615.77
7.1.120.0060.01315.54
7.1.110.0060.00315.72
7.1.100.0070.01315.39
7.1.90.0100.01015.68
7.1.80.0030.00915.75
7.1.70.0050.00516.38
7.1.60.0080.00817.58
7.1.50.0070.00516.10
7.1.40.0040.00715.67
7.1.30.0070.00715.76
7.1.20.0090.00615.76
7.1.10.0070.00715.63
7.1.00.0050.04019.10
7.0.330.0070.01015.25
7.0.320.0060.00914.93
7.0.310.0000.01515.19
7.0.300.0000.01415.20
7.0.290.0040.00815.43
7.0.280.0060.00915.29
7.0.270.0060.00615.21
7.0.260.0060.00915.07
7.0.250.0060.01115.42
7.0.240.0110.00415.04
7.0.230.0060.00915.23
7.0.220.0030.01215.22
7.0.210.0030.00715.29
7.0.200.0080.01115.88
7.0.190.0090.00315.09
7.0.180.0040.00415.41
7.0.170.0060.00615.23
7.0.160.0070.01015.30
7.0.150.0030.01015.20
7.0.140.0070.03518.52
7.0.130.0030.01015.27
7.0.120.0050.00815.45
7.0.110.0080.00615.08
7.0.100.0200.02717.69
7.0.90.0280.03817.71
7.0.80.0220.04517.82
7.0.70.0220.04517.57
7.0.60.0220.04217.68
7.0.50.0270.04217.94
7.0.40.0070.02716.79
7.0.30.0050.04416.69
7.0.20.0070.02716.67
7.0.10.0050.04316.65
7.0.00.0070.04516.67
5.6.400.0030.01614.05
5.6.390.0040.01114.18
5.6.380.0100.00614.32
5.6.370.0130.00314.24
5.6.360.0120.00314.43
5.6.350.0040.01514.48
5.6.340.0030.00914.39
5.6.330.0030.01114.33
5.6.320.0030.01414.46
5.6.310.0000.01414.27
5.6.300.0030.01114.39
5.6.290.0040.00814.22
5.6.280.0070.04217.64
5.6.270.0160.00014.25
5.6.260.0040.01114.14
5.6.250.0070.03217.52
5.6.240.0050.04717.43
5.6.230.0070.02917.50
5.6.220.0070.04317.43
5.6.210.0080.03517.53
5.6.200.0060.04617.63
5.6.190.0040.05217.75
5.6.180.0050.04217.78
5.6.170.0080.04317.64
5.6.160.0070.03217.60
5.6.150.0080.02317.71
5.6.140.0040.04917.65
5.6.130.0050.02817.75
5.6.120.0080.03617.66
5.6.110.0050.03517.70
5.6.100.0040.04717.87
5.6.90.0100.04017.84
5.6.80.0070.04017.35
5.6.70.0110.03617.40
5.6.60.0030.02817.46
5.6.50.0000.02817.44
5.6.40.0080.02317.43
5.6.30.0000.02717.34
5.6.20.0060.02217.38
5.6.10.0010.02517.31
5.6.00.0070.02317.26
5.5.380.0020.04917.26
5.5.370.0080.04317.34
5.5.360.0060.04317.28
5.5.350.0030.04817.29
5.5.340.0070.03017.72
5.5.330.0050.04217.59
5.5.320.0050.04817.38
5.5.310.0070.04217.67
5.5.300.0070.04617.68
5.5.290.0070.04517.61
5.5.280.0060.02917.62
5.5.270.0130.02317.70
5.5.260.0080.04517.54
5.5.250.0060.04417.47
5.5.240.0070.03917.31
5.5.230.0050.04217.20
5.5.220.0040.04717.28
5.5.210.0050.02217.26
5.5.200.0070.03316.95
5.5.190.0030.02317.33
5.5.180.0090.02217.20
5.5.170.0070.00314.02
5.5.160.0030.03217.27
5.5.150.0020.02517.16
5.5.140.0070.02117.27
5.5.130.0080.03317.15
5.5.120.0090.02417.22
5.5.110.0040.03117.29
5.5.100.0050.02417.17
5.5.90.0020.02317.20
5.5.80.0090.01617.04
5.5.70.0030.02617.12
5.5.60.0090.03117.10
5.5.50.0050.02417.10
5.5.40.0040.02517.26
5.5.30.0080.02117.07
5.5.20.0020.02517.16
5.5.10.0130.01717.11
5.5.00.0110.01717.21
5.4.450.0030.04515.37
5.4.440.0000.03215.26
5.4.430.0030.04015.18
5.4.420.0070.02215.25
5.4.410.0070.03515.20
5.4.400.0030.02814.90
5.4.390.0070.04114.92
5.4.380.0050.02015.01
5.4.370.0050.01915.01
5.4.360.0070.02014.84
5.4.350.0050.02114.99
5.4.340.0030.02014.80
5.4.330.0000.01210.61
5.4.320.0020.02614.99
5.4.310.0020.02514.96
5.4.300.0060.02015.08
5.4.290.0020.02214.93
5.4.280.0050.02314.88
5.4.270.0030.02314.92
5.4.260.0080.01615.06
5.4.250.0050.02014.98
5.4.240.0020.02214.93
5.4.230.0050.02014.93
5.4.220.0030.02315.06
5.4.210.0030.02214.94
5.4.200.0050.02215.00
5.4.190.0050.03214.91
5.4.180.0030.02015.02
5.4.170.0020.02114.91
5.4.160.0050.02314.95
5.4.150.0070.02415.01
5.4.140.0040.02513.72
5.4.130.0010.02113.51
5.4.120.0020.01913.61
5.4.110.0020.02313.71
5.4.100.0040.01613.73
5.4.90.0050.01713.74
5.4.80.0040.02013.64
5.4.70.0000.02013.75
5.4.60.0030.02113.69
5.4.50.0020.02513.69
5.4.40.0020.02313.78
5.4.30.0030.03713.81
5.4.20.0030.02013.73
5.4.10.0020.02313.72
5.4.00.0050.02113.32
5.3.290.0020.02412.63
5.3.280.0020.02012.70
5.3.270.0020.02112.70
5.3.260.0030.02512.71
5.3.250.0000.02812.67
5.3.240.0020.02012.51
5.3.230.0040.02112.63
5.3.220.0040.02012.57
5.3.210.0030.01812.70
5.3.200.0020.02312.61
5.3.190.0000.02812.73
5.3.180.0070.01712.72
5.3.170.0030.02212.77
5.3.160.0050.02112.76
5.3.150.0030.02012.74
5.3.140.0030.02312.67
5.3.130.0020.02912.59
5.3.120.0030.02212.49
5.3.110.0050.02012.70
5.3.100.0040.02012.52
5.3.90.0080.02212.40
5.3.80.0060.03612.36
5.3.70.0070.03712.27
5.3.60.0020.03212.41
5.3.50.0040.04212.35
5.3.40.0090.03312.22
5.3.30.0040.02812.32
5.3.20.0090.03411.99
5.3.10.0050.03612.14
5.3.00.0080.02812.08

preferences:
38.4 ms | 401 KiB | 5 Q