3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Social network site where you can see a list of users and "follow" them. */ // setup the `Db` object: require_once 'lib/db.php'; // setup the `Session` object: require_once 'lib/session.php'; if (!Session::isLoggedIn()) { throw new Exception('You must be logged in to view this page'); } // display a user as html function displayUser ($user, $tag='div') { $full_name = $user->first_name . ' ' . $user->last_name; // get user's avatar from 3rd-party avatar service, based on which environment we're running in if (strpos($_SERVER['SERVER_NAME'], 'localhost') !== false) { $avatar_data = json_decode(file_get_contents('http://dev.avatars.com/user/' . $user->id)); } else { $avatar_data = json_decode(file_get_contents('http://www.avatars.com/user/' . $user->id)); } ?> <<?= $tag ?> class="user"> <a href="/users/<?= $user->id ?>"> <img src="<?= $avatar_data->url ?>"> <?= $full_name ?> </a> <a href="?follow=<?= $user->id ?>">Follow</a> </<?= $tag ?>> <?php } // check that a user ID is valid (must be a number) function validateUserId ($id) { if (!is_numeric($id)) { // log invalid user IDs to help us track down bugs. error_log("Invalid user id: {$id}"); return false; } return true; } // load the user's custom theme config $theme_filename = dirname(__FILE__) . 'themes/' . Session::getLoggedInUser()->theme_id . '.php'; if (file_exists($theme_filename)) { require $theme_filename; } // show all users if (isset($_GET['show-all'])) { echo '<ul>'; foreach (Db::getUsers() as $user) { displayUser($user, 'li'); } echo '</ul>'; } // show a single user else if (isset($_GET['id'])) { $id = $_GET['id']; if (!validateUserId($id)) throw new Exception('Please select a valid user ID'); $user = Db::getUserById($id); if (!$user) throw new Exception('Sorry, user not found'); displayUser($user); } // follow a user else if (isset($_GET['follow'])) { $id = $_GET['follow']; if (!validateUserId($id)) throw new Exception('Please select a valid user ID'); $user_to_follow = Db::getUserById($id); if (!$user_to_follow) throw new Exception('Sorry, user not found'); $current_user = Session::getLoggedInUser(); try { $current_user->follow($user_to_follow); } catch (Exception $e) { throw new Exception('Unable to follow at this time'); } // inform the user that they have a new follower. mail( $user_to_follow->email, 'You have a new follower!', "{$current_user->username} is now following you.", 'From: notifications@awesome-social-network.com' ); // also update the activity feed, so users can see what others are doing on the site. Db::updateActivityFeed(array( 'action' => 'follow', 'from' => $current_user->id, 'to' => $user_to_follow->id, 'timestamp' => time(), )); echo 'Followed! <a href="/">Return to home</a>'; } ?>
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 10
Branch analysis from position: 6
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 25
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 41
Branch analysis from position: 28
2 jumps found. (Code = 77) Position 1 = 32, Position 2 = 38
Branch analysis from position: 32
2 jumps found. (Code = 78) Position 1 = 33, Position 2 = 38
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 129
Branch analysis from position: 129
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 38
Branch analysis from position: 41
2 jumps found. (Code = 43) Position 1 = 44, Position 2 = 70
Branch analysis from position: 44
2 jumps found. (Code = 43) Position 1 = 52, Position 2 = 56
Branch analysis from position: 52
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 56
2 jumps found. (Code = 43) Position 1 = 62, Position 2 = 66
Branch analysis from position: 62
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 66
1 jumps found. (Code = 42) Position 1 = 129
Branch analysis from position: 129
Branch analysis from position: 70
2 jumps found. (Code = 43) Position 1 = 73, Position 2 = 129
Branch analysis from position: 73
2 jumps found. (Code = 43) Position 1 = 81, Position 2 = 85
Branch analysis from position: 81
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 85
2 jumps found. (Code = 43) Position 1 = 91, Position 2 = 95
Branch analysis from position: 91
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 95
1 jumps found. (Code = 42) Position 1 = 107
Branch analysis from position: 107
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 129
Branch analysis from position: 25
Found catch point at position: 102
Branch analysis from position: 102
2 jumps found. (Code = 107) Position 1 = 103, Position 2 = -2
Branch analysis from position: 103
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/7SQgu
function name:  (null)
number of ops:  130
compiled vars:  !0 = $theme_filename, !1 = $user, !2 = $id, !3 = $user_to_follow, !4 = $current_user, !5 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   INCLUDE_OR_EVAL                                          'lib%2Fdb.php', REQUIRE_ONCE
   11     1        INCLUDE_OR_EVAL                                          'lib%2Fsession.php', REQUIRE_ONCE
   13     2        INIT_STATIC_METHOD_CALL                                  'Session', 'isLoggedIn'
          3        DO_FCALL                                      0  $8      
          4        BOOL_NOT                                         ~9      $8
          5      > JMPZ                                                     ~9, ->10
   14     6    >   NEW                                              $10     'Exception'
          7        SEND_VAL_EX                                              'You+must+be+logged+in+to+view+this+page'
          8        DO_FCALL                                      0          
          9      > THROW                                         0          $10
   52    10    >   INIT_FCALL                                               'dirname'
         11        SEND_VAL                                                 '%2Fin%2F7SQgu'
         12        DO_ICALL                                         $12     
         13        CONCAT                                           ~13     $12, 'themes%2F'
         14        INIT_STATIC_METHOD_CALL                                  'Session', 'getLoggedInUser'
         15        DO_FCALL                                      0  $14     
         16        FETCH_OBJ_R                                      ~15     $14, 'theme_id'
         17        CONCAT                                           ~16     ~13, ~15
         18        CONCAT                                           ~17     ~16, '.php'
         19        ASSIGN                                                   !0, ~17
   53    20        INIT_FCALL                                               'file_exists'
         21        SEND_VAR                                                 !0
         22        DO_ICALL                                         $19     
         23      > JMPZ                                                     $19, ->25
   54    24    >   INCLUDE_OR_EVAL                                          !0, REQUIRE
   58    25    >   FETCH_IS                                         ~21     '_GET'
         26        ISSET_ISEMPTY_DIM_OBJ                         0          ~21, 'show-all'
         27      > JMPZ                                                     ~22, ->41
   59    28    >   ECHO                                                     '%3Cul%3E'
   60    29        INIT_STATIC_METHOD_CALL                                  'Db', 'getUsers'
         30        DO_FCALL                                      0  $23     
         31      > FE_RESET_R                                       $24     $23, ->38
         32    > > FE_FETCH_R                                               $24, !1, ->38
   61    33    >   INIT_FCALL                                               'displayuser'
         34        SEND_VAR                                                 !1
         35        SEND_VAL                                                 'li'
         36        DO_FCALL                                      0          
   60    37      > JMP                                                      ->32
         38    >   FE_FREE                                                  $24
   63    39        ECHO                                                     '%3C%2Ful%3E'
         40      > JMP                                                      ->129
   66    41    >   FETCH_IS                                         ~26     '_GET'
         42        ISSET_ISEMPTY_DIM_OBJ                         0          ~26, 'id'
         43      > JMPZ                                                     ~27, ->70
   67    44    >   FETCH_R                      global              ~28     '_GET'
         45        FETCH_DIM_R                                      ~29     ~28, 'id'
         46        ASSIGN                                                   !2, ~29
   68    47        INIT_FCALL                                               'validateuserid'
         48        SEND_VAR                                                 !2
         49        DO_FCALL                                      0  $31     
         50        BOOL_NOT                                         ~32     $31
         51      > JMPZ                                                     ~32, ->56
         52    >   NEW                                              $33     'Exception'
         53        SEND_VAL_EX                                              'Please+select+a+valid+user+ID'
         54        DO_FCALL                                      0          
         55      > THROW                                         0          $33
   69    56    >   INIT_STATIC_METHOD_CALL                                  'Db', 'getUserById'
         57        SEND_VAR_EX                                              !2
         58        DO_FCALL                                      0  $35     
         59        ASSIGN                                                   !1, $35
   70    60        BOOL_NOT                                         ~37     !1
         61      > JMPZ                                                     ~37, ->66
         62    >   NEW                                              $38     'Exception'
         63        SEND_VAL_EX                                              'Sorry%2C+user+not+found'
         64        DO_FCALL                                      0          
         65      > THROW                                         0          $38
   72    66    >   INIT_FCALL                                               'displayuser'
         67        SEND_VAR                                                 !1
         68        DO_FCALL                                      0          
         69      > JMP                                                      ->129
   75    70    >   FETCH_IS                                         ~41     '_GET'
         71        ISSET_ISEMPTY_DIM_OBJ                         0          ~41, 'follow'
         72      > JMPZ                                                     ~42, ->129
   76    73    >   FETCH_R                      global              ~43     '_GET'
         74        FETCH_DIM_R                                      ~44     ~43, 'follow'
         75        ASSIGN                                                   !2, ~44
   77    76        INIT_FCALL                                               'validateuserid'
         77        SEND_VAR                                                 !2
         78        DO_FCALL                                      0  $46     
         79        BOOL_NOT                                         ~47     $46
         80      > JMPZ                                                     ~47, ->85
         81    >   NEW                                              $48     'Exception'
         82        SEND_VAL_EX                                              'Please+select+a+valid+user+ID'
         83        DO_FCALL                                      0          
         84      > THROW                                         0          $48
   78    85    >   INIT_STATIC_METHOD_CALL                                  'Db', 'getUserById'
         86        SEND_VAR_EX                                              !2
         87        DO_FCALL                                      0  $50     
         88        ASSIGN                                                   !3, $50
   79    89        BOOL_NOT                                         ~52     !3
         90      > JMPZ                                                     ~52, ->95
         91    >   NEW                                              $53     'Exception'
         92        SEND_VAL_EX                                              'Sorry%2C+user+not+found'
         93        DO_FCALL                                      0          
         94      > THROW                                         0          $53
   81    95    >   INIT_STATIC_METHOD_CALL                                  'Session', 'getLoggedInUser'
         96        DO_FCALL                                      0  $55     
         97        ASSIGN                                                   !4, $55
   83    98        INIT_METHOD_CALL                                         !4, 'follow'
         99        SEND_VAR_EX                                              !3
        100        DO_FCALL                                      0          
        101      > JMP                                                      ->107
   84   102  E > > CATCH                                       last         'Exception'
   85   103    >   NEW                                              $58     'Exception'
        104        SEND_VAL_EX                                              'Unable+to+follow+at+this+time'
        105        DO_FCALL                                      0          
        106      > THROW                                         0          $58
   89   107    >   INIT_FCALL                                               'mail'
   90   108        FETCH_OBJ_R                                      ~60     !3, 'email'
        109        SEND_VAL                                                 ~60
   91   110        SEND_VAL                                                 'You+have+a+new+follower%21'
   92   111        FETCH_OBJ_R                                      ~61     !4, 'username'
        112        NOP                                                      
        113        FAST_CONCAT                                      ~62     ~61, '+is+now+following+you.'
        114        SEND_VAL                                                 ~62
   93   115        SEND_VAL                                                 'From%3A+notifications%40awesome-social-network.com'
        116        DO_ICALL                                                 
   97   117        INIT_STATIC_METHOD_CALL                                  'Db', 'updateActivityFeed'
   98   118        INIT_ARRAY                                       ~64     'follow', 'action'
   99   119        FETCH_OBJ_R                                      ~65     !4, 'id'
        120        ADD_ARRAY_ELEMENT                                ~64     ~65, 'from'
  100   121        FETCH_OBJ_R                                      ~66     !3, 'id'
        122        ADD_ARRAY_ELEMENT                                ~64     ~66, 'to'
  101   123        INIT_FCALL                                               'time'
        124        DO_ICALL                                         $67     
        125        ADD_ARRAY_ELEMENT                                ~64     $67, 'timestamp'
        126        SEND_VAL_EX                                              ~64
        127        DO_FCALL                                      0          
  104   128        ECHO                                                     'Followed%21+%3Ca+href%3D%22%2F%22%3EReturn+to+home%3C%2Fa%3E'
  107   129    > > RETURN                                                   1

Function displayuser:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 25
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7SQgu
function name:  displayUser
number of ops:  51
compiled vars:  !0 = $user, !1 = $tag, !2 = $full_name, !3 = $avatar_data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      'div'
   19     2        FETCH_OBJ_R                                      ~4      !0, 'first_name'
          3        CONCAT                                           ~5      ~4, '+'
          4        FETCH_OBJ_R                                      ~6      !0, 'last_name'
          5        CONCAT                                           ~7      ~5, ~6
          6        ASSIGN                                                   !2, ~7
   22     7        INIT_FCALL                                               'strpos'
          8        FETCH_R                      global              ~9      '_SERVER'
          9        FETCH_DIM_R                                      ~10     ~9, 'SERVER_NAME'
         10        SEND_VAL                                                 ~10
         11        SEND_VAL                                                 'localhost'
         12        DO_ICALL                                         $11     
         13        TYPE_CHECK                                  1018          $11
         14      > JMPZ                                                     ~12, ->25
   23    15    >   INIT_FCALL                                               'json_decode'
         16        INIT_FCALL                                               'file_get_contents'
         17        FETCH_OBJ_R                                      ~13     !0, 'id'
         18        CONCAT                                           ~14     'http%3A%2F%2Fdev.avatars.com%2Fuser%2F', ~13
         19        SEND_VAL                                                 ~14
         20        DO_ICALL                                         $15     
         21        SEND_VAR                                                 $15
         22        DO_ICALL                                         $16     
         23        ASSIGN                                                   !3, $16
         24      > JMP                                                      ->34
   26    25    >   INIT_FCALL                                               'json_decode'
         26        INIT_FCALL                                               'file_get_contents'
         27        FETCH_OBJ_R                                      ~18     !0, 'id'
         28        CONCAT                                           ~19     'http%3A%2F%2Fwww.avatars.com%2Fuser%2F', ~18
         29        SEND_VAL                                                 ~19
         30        DO_ICALL                                         $20     
         31        SEND_VAR                                                 $20
         32        DO_ICALL                                         $21     
         33        ASSIGN                                                   !3, $21
   30    34    >   ECHO                                                     '++++%3C'
         35        ECHO                                                     !1
         36        ECHO                                                     '+class%3D%22user%22%3E%0A%09%3Ca+href%3D%22%2Fusers%2F'
   31    37        FETCH_OBJ_R                                      ~23     !0, 'id'
         38        ECHO                                                     ~23
         39        ECHO                                                     '%22%3E%0A%09++++%3Cimg+src%3D%22'
   32    40        FETCH_OBJ_R                                      ~24     !3, 'url'
         41        ECHO                                                     ~24
         42        ECHO                                                     '%22%3E%0A%09++++'
   33    43        ECHO                                                     !2
   34    44        ECHO                                                     '%09%3C%2Fa%3E%0A%09%3Ca+href%3D%22%3Ffollow%3D'
   35    45        FETCH_OBJ_R                                      ~25     !0, 'id'
         46        ECHO                                                     ~25
         47        ECHO                                                     '%22%3EFollow%3C%2Fa%3E%0A++++%3C%2F'
   36    48        ECHO                                                     !1
         49        ECHO                                                     '%3E%0A'
   38    50      > RETURN                                                   null

End of function displayuser

Function validateuserid:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 12
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7SQgu
function name:  validateUserId
number of ops:  14
compiled vars:  !0 = $id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   RECV                                             !0      
   42     1        INIT_FCALL                                               'is_numeric'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $1      
          4        BOOL_NOT                                         ~2      $1
          5      > JMPZ                                                     ~2, ->12
   44     6    >   INIT_FCALL                                               'error_log'
          7        NOP                                                      
          8        FAST_CONCAT                                      ~3      'Invalid+user+id%3A+', !0
          9        SEND_VAL                                                 ~3
         10        DO_ICALL                                                 
   45    11      > RETURN                                                   <false>
   48    12    > > RETURN                                                   <true>
   49    13*     > RETURN                                                   null

End of function validateuserid

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162.81 ms | 1423 KiB | 35 Q