Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.24 KB | None | 0 0
  1. Notice: Undefined variable: link in /storage/ssd4/573/2779573/public_html/pt-5-login.php on line 47
  2.  
  3. Warning: mysqli_prepare() expects parameter 1 to be mysqli, null given in /storage/ssd4/573/2779573/public_html/pt-5-login.php on line 47
  4.  
  5. Warning: mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, null given in /storage/ssd4/573/2779573/public_html/pt-5-login.php on line 105
  6.  
  7. Notice: Undefined variable: link in /storage/ssd4/573/2779573/public_html/pt-5-login.php on line 110
  8.  
  9. Warning: mysqli_close() expects parameter 1 to be mysqli, null given in /storage/ssd4/573/2779573/public_html/pt-5-login.php on line 110
  10.  
  11. <?php
  12.  
  13. /* Database credentials. Assuming you are running MySQL
  14. server with default setting (user 'root' with no password) */
  15.  
  16. define("DB_SERVER", "localhost");
  17. define("DB_USERNAME", "id2779573_root");
  18. define("DB_PASSWORD", "jackr0ck246");
  19. define("DB_DATABASE", "id2779573_dj_sql");
  20.  
  21. // Attempt to connect to MySQL database
  22. $connect = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
  23.  
  24. // Check connection
  25. if($connect === false)
  26. {
  27. die("ERROR: Could not connect. " . mysqli_connect_error());
  28. }
  29.  
  30. ?>
  31.  
  32. <!-- ----------------------------------------------------------------------- */
  33. /* Taken from "Tutorial Republic - PHP MySQL Login System" -
  34. /* ------------------------------------------------------------------------ -->
  35.  
  36. ----PHP CODE----
  37.  
  38. <?php
  39. // Include config file
  40. require_once 'config.php';
  41.  
  42. // Define variables and initialize with empty values
  43. $username = $password = $confirm_password = "";
  44. $username_err = $password_err = $confirm_password_err = "";
  45.  
  46. // Processing form data when form is submitted
  47. if($_SERVER["REQUEST_METHOD"] == "POST")
  48. {
  49.  
  50. // Validate username
  51. if(empty(trim($_POST["username"])))
  52. {
  53. $username_err = "Please enter a username.";
  54. }
  55.  
  56. else
  57. {
  58.  
  59. // Prepare a select statement
  60. $sql = "SELECT id FROM users WHERE username = ?";
  61.  
  62. if($stmt = mysqli_prepare($connect, $sql))
  63. {
  64.  
  65. // Bind variables to the prepared statement as parameters
  66. mysqli_stmt_bind_param($stmt, "s", $param_username);
  67.  
  68. // Set parameters
  69. $param_username = trim($_POST["username"]);
  70.  
  71. // Attempt to execute the prepared statement
  72. if(mysqli_stmt_execute($stmt))
  73. {
  74.  
  75. /* store result */
  76. mysqli_stmt_store_result($stmt);
  77.  
  78. if(mysqli_stmt_num_rows($stmt) == 1)
  79. {
  80. $username_err = "This username is already taken.";
  81. }
  82.  
  83. else
  84. {
  85. $username = trim($_POST["username"]);
  86. }
  87.  
  88. }
  89.  
  90. else
  91. {
  92. echo "Oops! Something went wrong. Please try again later.";
  93. }
  94.  
  95. }
  96.  
  97.  
  98. // Close statement
  99. mysqli_stmt_close($stmt);
  100. }
  101.  
  102. // Validate password
  103. if(empty(trim($_POST['password'])))
  104. {
  105. $password_err = "Please enter a password.";
  106. }
  107.  
  108. elseif(strlen(trim($_POST['password'])) < 6)
  109. {
  110. $password_err = "Password must have atleast 6 characters.";
  111. }
  112.  
  113. else
  114. {
  115. $password = trim($_POST['password']);
  116. }
  117.  
  118. // Validate confirm password
  119. if(empty(trim($_POST["confirm_password"])))
  120. {
  121. $confirm_password_err = 'Please confirm password.';
  122. }
  123.  
  124. else
  125. {
  126. $confirm_password = trim($_POST['confirm_password']);
  127. if($password != $confirm_password)
  128. {
  129. $confirm_password_err = 'Password did not match.';
  130. }
  131. }
  132.  
  133. // Check input errors before inserting in database
  134. if(empty($username_err) && empty($password_err) && empty($confirm_password_err))
  135. {
  136.  
  137. // Prepare an insert statement
  138. $sql = "INSERT INTO users (username, password) VALUES (?, ?)";
  139.  
  140. if($stmt = mysqli_prepare($connect, $sql))
  141. {
  142.  
  143. // Bind variables to the prepared statement as parameters
  144. mysqli_stmt_bind_param($stmt, "ss", $param_username, $param_password);
  145.  
  146. // Set parameters
  147. $param_username = $username;
  148. $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
  149.  
  150. // Attempt to execute the prepared statement
  151. if(mysqli_stmt_execute($stmt))
  152.  
  153. {
  154. // Redirect to login page
  155. header("location: pt-5-login.php"); // LINE 123 - ERROR OCCURRED
  156. }
  157.  
  158. else
  159. {
  160. echo "Something went wrong. Please try again later.";
  161. }
  162. }
  163.  
  164. // Close statement
  165. mysqli_stmt_close($stmt);
  166. }
  167.  
  168. // Close connection
  169. mysqli_close($connect);
  170.  
  171. }
  172.  
  173. ?>
  174.  
  175.  
  176. <!-- ----------------------------------------------------------------------------------- */
  177. /* Temporary suspension of "Tutorial Republic - PHP MySQL Login System" Tutorial
  178. /* ------------------------------------------------------------------------------------ -->
  179.  
  180.  
  181.  
  182.  
  183. ----HTML CODE----
  184.  
  185. <!doctype html>
  186. <html>
  187. <head>
  188. <title>Learning Portal</title> <!-- CHANGE THESE IN ALL FILES RELATING TO THIS PROJECT! -->
  189. <meta name="description" content="Learning Portal">
  190. <meta name="keywords" content="Learning Portal EFREI">
  191. <meta name="author" content="D.Jackson">
  192. <link rel="stylesheet" type="text/css" href="NHS.css" media="screen">
  193. <link rel="icon" type="image/ico" href="favicon.ico">
  194. <link href="https://fonts(dot)googleapis(dot)com/css?family=Jura" rel="stylesheet">
  195. </head>
  196.  
  197. <!-- <div> is to group block elements for CSS formatting -->
  198.  
  199. <body>
  200. <div id="container">
  201. <header><a href="pt-5-home.htm">Register</a>
  202. </header>
  203. <!-- The htm. above which would connect to the main page was originally
  204. "mars.htm", then "NHS.htm" and is now called "pt-5-home.htm" -->
  205.  
  206. <!--
  207. <div id="main">
  208. <section>
  209. <p>Please enter your details:
  210. </p>
  211.  
  212. -->
  213.  
  214.  
  215.  
  216.  
  217. <!-- ----------------------------------------------------------------------- */
  218. /* Resuming "Tutorial Republic - PHP MySQL Login System"
  219. https://www.tutorialrepublic.com/php-tutorial/php-mysql-login-system.php
  220. /* ------------------------------------------------------------------------ -->
  221.  
  222. <!--
  223.  
  224. </head>
  225. <body>
  226.  
  227. -->
  228.  
  229. <div class="wrapper">
  230. <h2>Sign Up</h2>
  231. <p>Please fill this form to create an account.</p>
  232. <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  233.  
  234.  
  235. <div class="form-group
  236. <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
  237. <label>Username:<sup>*</sup></label>
  238. <input type="text" name="username"class="form-control" value="<?php echo $username; ?>">
  239. <span class="help-block"><?php echo $username_err; ?></span>
  240. </div>
  241.  
  242. <div class="form-group
  243. <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
  244. <label>Password:<sup>*</sup></label>
  245. <input type="password" name="password" class="form-control" value="<?php echo $password; ?>">
  246. <span class="help-block"><?php echo $password_err; ?></span>
  247. </div>
  248.  
  249. <div class="form-group
  250. <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
  251. <label>Confirm Password:<sup>*</sup></label>
  252. <input type="password" name="confirm_password" class="form-control" value="<?php echo $confirm_password; ?>">
  253. <span class="help-block"><?php echo $confirm_password_err; ?></span>
  254. </div>
  255.  
  256. <div class="form-group">
  257. <input type="submit" class="btn btn-primary" value="Submit">
  258. <input type="reset" class="btn btn-default" value="Reset">
  259. </div>
  260.  
  261. <p>Already have an account? <a href="pt-5-login.php">Login here</a>.</p>
  262. </form>
  263. </div>
  264. </body>
  265. </html>
  266.  
  267. <!-- ----------------------------------------------------------------------- */
  268. /* Taken from "Tutorial Republic - PHP MySQL Login System"
  269. /* ------------------------------------------------------------------------ -->
  270.  
  271. ----PHP CODE----
  272.  
  273. <?php
  274. // Include config file
  275. require_once 'config.php';
  276.  
  277. // Define variables and initialize with empty values
  278. $username = $password = "";
  279. $username_err = $password_err = "";
  280.  
  281. // Processing form data when form is submitted
  282. if($_SERVER["REQUEST_METHOD"] == "POST")
  283. {
  284.  
  285. // Check if username is empty
  286. if(empty(trim($_POST["username"])))
  287. {
  288. $username_err = 'Please enter username.';
  289. }
  290.  
  291. else
  292. {
  293. $username = trim($_POST["username"]);
  294. }
  295.  
  296. // Check if password is empty
  297. if(empty(trim($_POST['password'])))
  298. {
  299. $password_err = 'Please enter your password.';
  300. }
  301.  
  302. else
  303. {
  304. $password = trim($_POST['password']);
  305. }
  306.  
  307. // Validate credentials
  308. if(empty($username_err) && empty($password_err))
  309. {
  310.  
  311. // Prepare a select statement
  312. $sql = "SELECT username, password FROM users WHERE username = ?";
  313.  
  314. if($stmt = mysqli_prepare($link, $sql)) // LINE 47 - ERROR OCCURRED
  315. }
  316. {
  317.  
  318. // Bind variables to the prepared statement as parameters
  319. mysqli_stmt_bind_param($stmt, "s", $param_username);
  320.  
  321. // Set parameters
  322. $param_username = $username;
  323.  
  324. // Attempt to execute the prepared statement
  325. if(mysqli_stmt_execute($stmt))
  326. {
  327.  
  328. // Store result
  329. mysqli_stmt_store_result($stmt);
  330.  
  331. // Check if username exists, if yes then verify password
  332. if(mysqli_stmt_num_rows($stmt) == 1)
  333. {
  334.  
  335. // Bind result variables
  336. mysqli_stmt_bind_result($stmt, $username, $hashed_password);
  337. if(mysqli_stmt_fetch($stmt))
  338. {
  339. if(password_verify($password, $hashed_password))
  340. {
  341.  
  342. /* Password is correct, so start a new session and
  343. save the username to the session */
  344.  
  345. session_start();
  346. $_SESSION['username'] = $username;
  347. header("location: welcome.php");
  348. }
  349.  
  350. else
  351. {
  352. // Display an error message if password is not valid
  353. $password_err = 'The password you entered was not valid.';
  354. }
  355. }
  356. }
  357.  
  358. else
  359. {
  360. // Display an error message if username doesn't exist
  361. $username_err = 'No account found with that username.';
  362. }
  363. }
  364.  
  365. else
  366. {
  367. echo "Oops! Something went wrong. Please try again later.";
  368. }
  369.  
  370. }
  371.  
  372. // Close statement
  373. mysqli_stmt_close($stmt); // LINE 105 - ERROR OCCOURED
  374. }
  375.  
  376. }
  377.  
  378. // Close connection
  379. mysqli_close($link); // LINE 110 - ERROR OCCOURED
  380. }
  381.  
  382. }
  383.  
  384. ?>
  385.  
  386.  
  387.  
  388.  
  389. <!-- ----------------------------------------------------------------------------------- */
  390. /* Temporary suspension of "Tutorial Republic - PHP MySQL Login System" Tutorial
  391. /* ------------------------------------------------------------------------------------ -->
  392.  
  393. ----HTML CODE----
  394.  
  395. <!doctype html>
  396. <html>
  397. <head>
  398. <title>Learning Portal</title> <!-- CHANGE THESE IN ALL FILES RELATING TO THIS PROJECT! -->
  399. <meta name="description" content="Learning Portal">
  400. <meta name="keywords" content="Learning Portal EFREI">
  401. <meta name="author" content="D.Jackson">
  402. <link rel="stylesheet" type="text/css" href="NHS.css" media="screen">
  403. <link rel="icon" type="image/ico" href="favicon.ico">
  404. <link href="https://fonts(dot)googleapis(dot)com/css?family=Jura" rel="stylesheet">
  405. </head>
  406.  
  407. <!-- <div> is to group block elements for CSS formatting -->
  408.  
  409. <body>
  410. <div id="container">
  411. <header><a href="pt-5-home.htm">Login</a>
  412. </header>
  413. <!-- The htm. above which would connect to the main page was originally
  414. "mars.htm", then "NHS.htm" and is now called "pt-5-home.htm" -->
  415.  
  416. <!--
  417.  
  418. <div id="main">
  419. <section>
  420. <p>Please enter your details:
  421. </p>
  422.  
  423. -->
  424.  
  425.  
  426.  
  427.  
  428. <!-- ----------------------------------------------------------------------- */
  429. /* Resuming "Tutorial Republic - PHP MySQL Login System"
  430. https://www.tutorialrepublic.com/php-tutorial/php-mysql-login-system.php
  431. /* ------------------------------------------------------------------------ -->
  432.  
  433. <body>
  434. <div class="wrapper">
  435. <h2>Login</h2>
  436. <p>Please fill in your credentials to login.</p>
  437. <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  438. <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
  439. <label>Username:<sup>*</sup></label>
  440. <input type="text" name="username"class="form-control" value="<?php echo $username; ?>">
  441. <span class="help-block"><?php echo $username_err; ?></span>
  442. </div>
  443.  
  444. <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
  445. <label>Password:<sup>*</sup></label>
  446. <input type="password" name="password" class="form-control">
  447. <span class="help-block"><?php echo $password_err; ?></span>
  448. </div>
  449.  
  450. <div class="form-group">
  451. <input type="submit" class="btn btn-primary" value="Submit">
  452. </div>
  453. <p>Don't have an account? <a href="pt-5-register.php">Sign up now</a>.</p>
  454. </form>
  455. </div>
  456. </body>
  457. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement