Advertisement
Guest User

Untitled

a guest
Oct 11th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. use CGI qw/:all/;
  4. use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
  5.  
  6. my $value = 99;
  7.  
  8. $max_number_to_guess = 99;
  9.  
  10. sub main() {
  11. # print start of HTML ASAP to assist debugging if there is an error in the script
  12. print page_header();
  13.  
  14. # Now tell CGI::Carp to embed any warning in HTML
  15. warningsToBrowser(1);
  16.  
  17. $username = param('username') || '';
  18. $password = param('password') || '';
  19.  
  20. # remove any non-word characters from username
  21. # another malicious user could include ../ in username
  22. $username =~ s/\W//g;
  23. # limit username to 32 word characters
  24. $username = substr $username, 0, 32;
  25.  
  26. # variable to check if user is authenticated
  27. $check = 0;
  28.  
  29.  
  30. if (!$username || !$password) {
  31. print login_form();
  32. print "hello world!\n";
  33. }
  34. #check username and password
  35. if ($username ne '' && $password ne '') {
  36. if (open(F, "<accounts/$username/password")) {
  37. $line = <F>;
  38. chomp $line;
  39. if ($line eq $password) {
  40. $check = 1;
  41. $guess = param('guess') || '';
  42. # remove any non-digit characters from guess
  43. $guess =~ s/\D//g;
  44.  
  45. $number_to_guess = param('number_to_guess') || '';
  46. $number_to_guess =~ s/\D//g;
  47.  
  48. if ($number_to_guess eq '') {
  49. $number_to_guess = 1 + int(rand $max_number_to_guess);
  50. print "I've thought of a number\n";
  51. print guess_number_form($username, $password, $number_to_guess);
  52. } elsif ($guess eq '') {
  53. print guess_number_form($username, $password, $number_to_guess);
  54. } elsif ($guess == $number_to_guess) {
  55. print "You guessed right, it was $number_to_guess.\n";
  56. print guess_number_form($username, $password, $number_to_guess);
  57. } elsif ($guess < $number_to_guess) {
  58. print "Its higher than $guess.\n";
  59. print guess_number_form($username, $password, $number_to_guess);
  60. } else {
  61. print "Its lower than $guess.\n";
  62. print guess_number_form($username, $password, $number_to_guess);
  63. }
  64. } else {
  65. print "Incorrect password!\n";
  66. }
  67. } else {
  68. print "Unknown username!\n";
  69. }
  70. }
  71.  
  72.  
  73. print page_trailer();
  74. }
  75.  
  76. # form to allow user to supply username/password
  77.  
  78. sub login_form {
  79. return <<eof;
  80. <form method="POST" action="">
  81. Username: <input type="textfield" name="username">
  82. <p>
  83. Password: <input type="password" name="password">
  84. <p>
  85. <input type="submit" value="Login">
  86. </form>
  87. eof
  88. }
  89.  
  90. #
  91. # form to allow user to guess a number
  92. #
  93. # Pass username & password to next invocation as hidden
  94. # field so user doesn't have to login again
  95. #
  96.  
  97. sub guess_number_form {
  98. my ($username, $password, $number_to_guess) = @_;
  99. return <<eof;
  100. <form method="POST" action="">
  101. Enter a guess between 1 and $max_number_to_guess (inclusive):
  102. <input type="textfield" name="guess">
  103. <input type="hidden" name="username" value="$username">
  104. <input type="hidden" name="password" value="$password">
  105. <input type="hidden" name="number_to_guess" value ="$number_to_guess">
  106. </form>
  107. eof
  108. }
  109.  
  110. #
  111. # form to allow user to go to a new game
  112. #
  113. sub new_game_form {
  114. my ($username, $password) = @_;
  115. return <<eof;
  116. <form method="POST" action="">
  117. <input type="submit" value="Play Again">
  118. <input type="hidden" name="username" value="$username">
  119. <input type="hidden" name="password" value="$password">
  120. </form>
  121. eof
  122. }
  123.  
  124.  
  125.  
  126. #
  127. # HTML placed at the top of every page
  128. #
  129. sub page_header {
  130. return <<eof
  131. Content-Type: text/html;charset=utf-8
  132.  
  133. <!DOCTYPE html>
  134. <html lang="en">
  135. <head>
  136. <title>Guess A Number</title>
  137. </head>
  138. <body>
  139. eof
  140. }
  141.  
  142. #
  143. #print number functions
  144. #
  145. sub guess_text(){
  146. print "I've thought of a number\n";
  147. print "Enter a guess between 1 and 99 (inclusive):";
  148. }
  149.  
  150. #
  151. # HTML placed at the bottom of every page
  152. #
  153. sub page_trailer {
  154. return "</body>\n</html>\n";
  155. }
  156.  
  157.  
  158. main();
  159. exit(0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement