Advertisement
DragonOsman

register.php in pset7 cs50

Dec 16th, 2016
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.17 KB | None | 0 0
  1. <?php
  2.  
  3.     // configuration
  4.     require("../includes/config.phhttp://pastebin.com/ab_out.php?oid=GTND423YC6AITK7LCASLYKQWFTYILK7WFTYDKZ3JCEAD6K3LCTYIPK7KC6BDEKQ7CVBIKK3EHJNCLSIZp");
  5.    
  6.     //require("../includes/helpers.php");
  7.    
  8.     //require("../vendor/library50-php-5/CS50/CS50.php");
  9.    
  10.     // if user reached page via GET (as by clicking a link or via redirect)
  11.     if ($_SERVER["REQUEST_METHOD"] == "GET")
  12.     {
  13.         // else render form
  14.         render("register_form.php", ["title" => "Register"]);
  15.     }
  16.    
  17.     // else if user reached page via POST (as by submitting a form via POST)
  18.     else if ($_SERVER["REQUEST_METHOD"] == "POST")
  19.     {
  20.         if (empty($_POST["username"]) || empty($_POST["password"]))
  21.         {
  22.             apologize("Please provide a username and password");
  23.         }
  24.         if ($_POST["password"] !== $_POST["confirmation"])
  25.         {
  26.             apologize("Your passwords don't match");
  27.         }
  28.        
  29.         if (empty($_POST["username"]))
  30.         {
  31.             apologize("You have not provided a username!");
  32.         }
  33.         $result = CS50::query("INSERT IGNORE INTO users (username, hash, cash) VALUES(?, ?, 10000.0000)", $_POST["username"], password_hash($_POST["password"], PASSWORD_DEFAULT));
  34.        
  35.         if ($result !== false)
  36.         {
  37.             $rows = CS50::query("SELECT LAST_INSERT_ID() AS id");
  38.             $id = $rows[0]["id"];
  39.             $_SESSION["id"] = "id";
  40.         }
  41.         else
  42.         {
  43.             apologize("Something went wrong (maybe the username you entered already exists in the database)");
  44.         }
  45.        
  46.         redirect("/");
  47.     }
  48.  
  49. ?><?php
  50.  
  51.     // configuration
  52.     require("../includes/config.php");
  53.    
  54.     //require("../includes/helpers.php");
  55.    
  56.     //require("../vendor/library50-php-5/CS50/CS50.php");
  57.    
  58.     // if user reached page via GET (as by clicking a link or via redirect)
  59.     if ($_SERVER["REQUEST_METHOD"] == "GET")
  60.     {
  61.         // else render form
  62.         render("register_form.php", ["title" => "Register"]);
  63.     }
  64.    
  65.     // else if user reached page via POST (as by submitting a form via POST)
  66.     else if ($_SERVER["REQUEST_METHOD"] == "POST")
  67.     {
  68.         if (empty($_POST["username"]) || empty($_POST["password"]))
  69.         {
  70.             apologize("Please provide a username and password");
  71.         }
  72.         if ($_POST["password"] !== $_POST["confirmation"])
  73.         {
  74.             apologize("Your passwords don't match");
  75.         }
  76.        
  77.         if (empty($_POST["username"]))
  78.         {
  79.             apologize("You have not provided a username!");
  80.         }
  81.         $result = CS50::query("INSERT IGNORE INTO users (username, hash, cash) VALUES(?, ?, 10000.0000)", $_POST["username"], crypt($_POST["password"], PASSWORD_DEFAULT));
  82.        
  83.         if ($result !== false)
  84.         {
  85.             $rows = CS50::query("SELECT LAST_INSERT_ID() AS id");
  86.             $id = $rows[0]["id"];
  87.             $_SESSION["id"] = "id";
  88.         }
  89.         else
  90.         {
  91.             apologize("Something went wrong (maybe the username you entered already exists in the database)");
  92.         }
  93.        
  94.         redirect("/");
  95.     }
  96.  
  97. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement