Here's a challenge: I have a piece of PHP code, and it's up to you to find the possible issues with it. Security issues, code issues as well as issues with different PHP configurations and versions. Look at the code from the Nucleus perspective: a PHP script which will be installed on many different systems (for Nucleus: PHP versions 4.0.6 and up), and should run out of the box.
Solutions can be found in a follow-up post.
About the script
It's a really lame script: it displays an input form, where you can enter your name. The script will then lookup a matching greeting from the database and display it. If no greeting can be found, it will display a simple "Welcome Again!".
There's one special case. If "It's karma" is entered in the form field, the script will simply display "Hi Karma!" and quit.
The script
1 <?
2 switch ($HTTP_POST_VARS['name'])
3 {
4 case "It's karma":
5 echo 'Hi Karma!';
6 exit;
7 case "":
8 showform();
9 exit;
10 default:
11 showgreeting();
12 showform();
13 exit;
14 }
15
16 function showForm()
17 {
18 echo '<form method="post">';
19 echo '<input name="name">';
20 echo '<input type="submit">';
21 }
22
23 function showGreeting()
24 {
25 global $name;
26
27 // get greeting from database
28 mysql_connect('localhost');
29 mysql_select_db('test');
30
31 $query = "select greeting from test "
32 $query .= where name='" . $name . "'";
33
34 $r = mysql_query($query);
35 if (mysql_num_rows($r) > 0)
36 {
37 $o = mysql_fetch_object($r);
38 $greeting = $o->greeting;
39 } else {
40 $greeting = 'Welcome again!';
41 }
42
43 mysql_close();
44
45 // each <%user%> will be replaced by the user name
46 $template = 'Hi <%user%>! <%greeting%>';
47
48 $text = str_replace('<%user%>',
$name, $template);
49 $text = str_replace('<%greeting%>',
$greeting, $text);
50
51 echo $text;
52 }
53
54 ?>

Comments
Add Comment