Advertisement
thotfrnk

final project part2.php

Feb 1st, 2024
673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 25.42 KB | None | 0 0
  1. //home_guest_nav.php
  2. <!--this will display on index.php for the guest (non-registered users) who wish to navigate photoshare-->
  3.   <div class="nav1">
  4.     <ul>
  5.       <li id="home"><a href="index.php"><img src="images/camera.png" height="32" width="32" alt="camera icon"> | PhotoShare</a></li>
  6.        <!--camera icon:
  7.     Flaticon. (2019, July 24). Camera free icons designed by Freepik. Flaticon. https://www.flaticon.com/free-icon/camera_1998342 -->
  8.       <li id="log"><a href="html/login.html">Login</a></li>
  9.       <li id="reg"><a href="php/reg_form.php">Sign Up</a></li>
  10.     </ul>
  11.   </div>
  12.  
  13.   <div class="nav2">
  14.     <ul>
  15.       <li><a href="php/gallery.php">Gallery</a></li>
  16.       <li><a href="html/contact.html">Contact</a></li>
  17.     </ul>
  18.     <form method="post" action="php/search.php" id="search_frm">
  19.  
  20.     <input type="text" placeholder="Search PhotoShare" name="search" id="search">
  21.  
  22.     <input type="submit" name="search_btn" value="Search" id="search_btn">
  23. </form>
  24.   </div>
  25.  
  26. //home_memeber_nav.php
  27. <!--this will display on all the php files for the members (registered users) who wish to navigate photoshare-->
  28.   <div class="nav1">
  29.     <ul>
  30.       <li><a href="index.php"><img src="images/camera.png" height="32" width="32"> | PhotoShare</a></li>
  31.        <!--camera icon:
  32.     Flaticon. (2019, July 24). Camera free icons designed by Freepik. Flaticon. https://www.flaticon.com/free-icon/camera_1998342 -->
  33.       <li id="log_out"><form action='index.php' method='get'>
  34.     <input type=submit value='logout' name='logout' id='logout'>
  35.     </form></li>
  36.  
  37.       <li id="profile">
  38.             <!--dropdown code:
  39.     W3Schools. (n.d.). How to - hoverable dropdown. How To Create a Hoverable Dropdown Menu. https://www.w3schools.com/howto/howto_css_dropdown.asp -->
  40.         <div class="dropdown">
  41.           <button class="dropbtn">
  42.             <?php echo $_SESSION['user']; ?>
  43.             <i class="fa fa-ceret-down"></i>
  44.           </button>
  45.           <div class="drp_content">
  46.             <a href="php/profile.php">Profile</a>
  47.             <a href="php/edit_profile.php">Edit Profile</a>
  48.           </div>
  49.         </div>
  50.       </li>
  51.     </ul>
  52.   </div>
  53.  
  54.   <div class="nav2">
  55.     <ul>
  56.       <li><a href="php/gallery.php">Gallery</a></li>
  57.       <li><a href="php/upload.php">New Post</a></li>
  58.       <li><a href="html/contact.html">Contact</a></li>
  59.     </ul>
  60.     <form method="post" action="php/search.php">
  61.  
  62.     <input type="text" placeholder="Search PhotoShare" name="search" id="search">
  63.  
  64.     <input type="submit" name="search_btn" value="Search" id="search_btn">
  65. </form>
  66.   </div>
  67.  
  68. //member_nav.php
  69. <!--this will display on the index.php page for the members (egistered users) who wish to navigate photoshare-->
  70.   <div class="nav1">
  71.     <ul>
  72.       <li><a href="../index.php"><img src="../images/camera.png" height="32" width="32"> | PhotoShare</a></li>
  73.        <!--camera icon:
  74.     Flaticon. (2019, July 24). Camera free icons designed by Freepik. Flaticon. https://www.flaticon.com/free-icon/camera_1998342 -->
  75.  
  76.       <li id="log_out"><form action='../index.php' method='get'>
  77.     <input type=submit value='logout' name='logout' id='logout'>
  78.     </form></li>
  79.  
  80.       <li id="profile">
  81.  
  82.       <!--dropdown code:
  83.     W3Schools. (n.d.). How to - hoverable dropdown. How To Create a Hoverable Dropdown Menu. https://www.w3schools.com/howto/howto_css_dropdown.asp -->
  84.         <div class="dropdown">
  85.           <button class="dropbtn">
  86.             <?php echo $_SESSION['user']; ?>
  87.             <i class="fa fa-ceret-down"></i>
  88.           </button>
  89.           <div class="drp_content">
  90.             <a href="profile.php">Profile</a>
  91.             <a href="edit_profile.php">Edit Profile</a>
  92.           </div>
  93.         </div>
  94.       </li>
  95.     </ul>
  96.   </div>
  97.  
  98.   <div class="nav2">
  99.     <ul>
  100.       <li><a href="gallery.php">Gallery</a></li>
  101.       <li><a href="upload.php">New Post</a></li>
  102.       <li><a href="../html/contact.html">Contact</a></li>
  103.     </ul>
  104.     <form method="post" action="search.php">
  105.  
  106.     <input type="text" placeholder="Search PhotoShare" name="search" id="search">
  107.  
  108.     <input type="submit" name="search_btn" value="Search" id="search_btn">
  109. </form>
  110.   </div>
  111.  
  112. //profile.php
  113. <?php
  114. //session info
  115. require('session.php');
  116. ?>
  117.  
  118. <!--a member's only page to view their profile which will pull all their posts from the post_info table on the database with the help of the session information-->
  119.  
  120. <!DOCTYPE html>
  121. <html lang="en">
  122. <head>
  123.   <meta charset="UTF-8">
  124.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  125.   <title>Profile</title>
  126.  
  127.   <link href="../css/style.css" rel="stylesheet" type="text/css">
  128.  
  129.   <link rel="icon" type="image/png" sizes="32x32" href="../images/favicon-32x32.png">
  130.  
  131. </head>
  132. <body class="box">
  133.  
  134. <!--Navigation bar-->
  135. <?php
  136.   require('member_nav.php');
  137. ?>
  138.  
  139. <h1 class="header_center"><?php echo $_SESSION['user']; ?></h1>
  140.  
  141. <?php
  142.  
  143. //database connection
  144. require('db_connect.php');
  145.  
  146. //query string
  147. $qry = "select * from post_info where author = '$_SESSION[user]'";
  148.  
  149. //execute the query
  150. $result = mysqli_query($conn, $qry);
  151.  
  152. if ($result) {
  153.  
  154.   if (mysqli_num_rows($result) > 0) {
  155.  
  156.     while ($row = mysqli_fetch_assoc($result)) {
  157.      
  158.       //displays all the user's posts
  159.       echo "<div class=\"gallery\">
  160.  
  161.      <img src='$row[picture]'  alt='User Upload' width='500' height='333'>
  162.  
  163.      <br>
  164.  
  165.      <div class=\"caption\">$row[caption]</div>
  166.  
  167.      <div class=\"author\"><strong>$row[author]</strong></div>
  168.  
  169.      <div class=\"date\"><strong>$row[post_date]</strong></div>
  170.      
  171.      </div>";
  172.  
  173.     }
  174.     //end while loop
  175.  
  176.   }
  177.   //end if 1 or more rows
  178.  
  179.   else
  180.     echo "<br>No posts avaliable.";
  181. }
  182. mysqli_close($conn);
  183. ?>
  184.  
  185. <?php
  186.  //footer
  187.  require('footer.php');
  188.  ?>
  189.  
  190. </body>
  191. </html>
  192.  
  193. //reg_form.php
  194. <?php
  195. //session info
  196. require('session.php');
  197. ?>
  198.  
  199. <!--registration page to sign up for photoshare-->
  200.  
  201. <!DOCTYPE html>
  202. <html lang="en">
  203. <head>
  204.   <meta charset="UTF-8">
  205.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  206.   <title>Register on PhotoShare</title>
  207.  
  208.   <link href="../css/style.css" rel="stylesheet" type="text/css">
  209.  
  210.   <link rel="icon" type="image/png" sizes="32x32" href="../images/favicon-32x32.png">
  211.  
  212. </head>
  213. <body>
  214.  
  215. <?php
  216.  
  217. //database connection
  218. require("db_connect.php");
  219.  
  220. //server-side validation
  221. //fixed the validation (i forgot a ; in my javascript file so i added it in) and added in the verify password input
  222.  
  223. //initalizing php variables to hold form data
  224.  
  225. $username = $email = $pword = $vpass = $fname = $lname = $age = $phone = "";
  226.  
  227. $userErr = $emailErr = $pwordErr = $vpass = $fnErr = $lnErr = $ageErr = $phoneErr = "";
  228.  
  229. $valid = true;
  230.  
  231. if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST["submit"])) {
  232.  
  233.  $vpass = $_POST["vpass"];
  234.  $pword = $_POST["pword"];
  235.  
  236.   if(!($pword===$vpass)) {
  237.     die("The passwords do not match. Please return to <a href='reg_form.php'>registration page</a>");
  238.   }
  239.  
  240.   //testing username
  241.  
  242.   //testing if username field is empty
  243.   if(empty($_POST["user_name"])) {
  244.     //error message will display if field is empty
  245.     $userErr = "Username is required.";
  246.     $valid = false;
  247.   }
  248.   //further assessment if field is not empty
  249.   else {
  250.     //retrieves username data
  251.     $username = $_POST["user_name"];
  252.     //cleans up username data
  253.     $username = test_input($username);
  254.  
  255.     //test data type and format using regular expression
  256.     if (!preg_match("/^[a-zA-Z0-9_-]{4,30}$/", $username)) {
  257.       $userErr = "Username must only contain no less than 4 characters, letters, numbers, _, and -.";
  258.  
  259.       $valid = false;
  260.     }
  261.   }
  262.  
  263.   //testing email
  264.  
  265.   //testing if email field is empty
  266.   if(empty($_POST["email"])) {
  267.     //error msg will display if field is empty
  268.     $emailErr = "Email is required.";
  269.     $valid = false;
  270. }
  271. //further assesment if field is not empty
  272. else {
  273. //retrieves email data
  274. $email = $_POST["email"];
  275. //clean up the email data
  276. $email = test_input($email);
  277.  
  278. //test data type and format using regular expression
  279. if(!preg_match("/^[a-zA-Z0-9.]{2,30}@[a-zA-Z0-9.]{2,20}.[a-zA-Z]{2,4}$/", $email)) {
  280.     //error msg will display if data doesn't match regex
  281.     $emailErr = "Invalid email address entered.";
  282.     $valid = false;
  283.  
  284. }
  285. }
  286.  
  287. //testing password
  288.  
  289. //testing if password field is empty
  290. if(empty($_POST["pword"])) {
  291.   //an error msg will dispay if field is empty
  292.   $pwordErr = "Password is required.";
  293.   $valid = false;
  294. }
  295. //futher assesssment if field is not empty
  296. else {
  297.   //retrives password data
  298.   $pword = $_POST["pword"];
  299.   //cleans up password data
  300.   $pword = test_input($pword);
  301.  
  302.   //test data type and format using regular expression
  303.   if(!preg_match("/^[a-zA-Z0-9@&!*%$]{10,60}$/", $pword)) {
  304.     //error msg will display if data doesn't match regex
  305.     $pwordErr = "Password must only contain letters, numbers, @, &, !, *, %, and $.";
  306.     $valid = false;
  307.   }
  308. }
  309.  
  310. //testing first name
  311.  
  312. //testing if fname field is empty
  313. if(empty($_POST["fname"])) {
  314.   //an error msg will dispay if field is empty
  315.   $fnErr = "First Name is required.";
  316.   $valid = false;
  317. }
  318. //further assessment if fname field isn't empty
  319. else {
  320.   //retrieves fname data
  321.   $fname = $_POST["fname"];
  322.   //clean up the fname data
  323.   $fname = test_input($fname);
  324.  
  325.   //test data type and format using regular expression
  326.   if(!preg_match("/^[a-zA-Z'!-]{2,30}$/", $fname)) {
  327.       //error msg will display if data doesn't match regex
  328.       $fnErr = "First name must only contain letters, ', !, and -.";
  329.       $valid = false;
  330.   }
  331. }
  332.  
  333. //testing last name
  334. //testing if lname field is empty
  335. if(empty($_POST["lname"])) {
  336.   //an error msg will dispay if field is empty
  337.   $lnErr = "Last Name is required.";
  338.   $valid = false;
  339. }
  340.  
  341.  
  342. //testing if lname field isn't empty
  343. if(!empty($_POST["lname"])) {
  344.   //retrives form data
  345.   $lname = $_POST["lname"];
  346.   //clean up lname data
  347.   $lname = test_input($lname);
  348.  
  349.   //test data type and format using regular expression
  350.   if(!preg_match("/^[a-zA-Z'!-]{2,50}$/", $lname)) {
  351.     //error msg will display if data doesn't match regex
  352.     $lnErr = "Last name must only contain letters, ', !, and -.";
  353.     $valid = false;
  354. }
  355. }
  356.  
  357. //testing age
  358.  
  359. //testing if the age field is empty
  360. if (empty($_POST["age"])) {
  361.   //error msy will display if age field is empty
  362.   $ageErr = "Age is required.";
  363. }
  364. //futher assessment if age field isn't empty
  365. else {
  366.   //retrieves age data from form
  367.   $age = $_POST["age"];
  368.   //cleans up age data
  369.   $age = test_input($age);
  370.  
  371.   //test data type and format using regular expression
  372.   if(!preg_match("/^[0-9]{2,3}$/", $age)) {
  373.       $ageErr = "Age must only contain numbers.";
  374.       $valid = false;
  375.     }
  376.     else {
  377.       //test if age is above 15 but under 100 years
  378.   if($age < 15 && $age > 100) {
  379.     $ageErr = "You must be over 15 to sign up.";
  380.     $valid = false;
  381.   }
  382.     }
  383. }
  384.  
  385. //testing phone number
  386.  
  387. //testing if the phone num field is empty
  388. if (empty($_POST["phone"])) {
  389.   //error msy will display if age field is empty
  390.   $phoneErr = "Phone number is required.";
  391. }
  392. //futher assessment if age field isn't empty
  393. else {
  394.   //retrieves age data from form
  395.   $phone = $_POST["phone"];
  396.   //cleans up age data
  397.   $phone = test_input($phone);
  398.  
  399.   //test data type and format using regular expression
  400.   if(!preg_match("/^\([0-9]{3}\)[0-9]{3}\-[0-9]{4}$/", $phone)) {
  401.     $phoneErr = "Phone number must be in the format: (000)000-0000.";
  402.     $valid = false;
  403.   }
  404. }
  405.  
  406. //for all valid data
  407. if($valid) {
  408.   //once all data is valid, it will insert the data into the database
  409.  
  410.   //encrypting password before it enters the database
  411.   $pword_hash = hash('sha256', $pword);
  412.  
  413.    //use PHP variables as input to MySQL query.
  414. $qry = "INSERT INTO user_info (username, email, pword, fname, lname, age, phone_num) VALUES ('$username', '$email', '$pword_hash', '$fname', '$lname', '$age', '$phone');";  
  415.  
  416. //execute the query    
  417. $result = mysqli_query($conn, $qry);
  418.  
  419. //check on the success of the query
  420.  if(!$result) echo 'Error occurred: ' . mysqli_error($conn) . '<br><br>';  
  421.  //close the connection
  422.  mysqli_close($conn);
  423.  
  424.  //once all the info is validated, the user will be sent to the login page to log into their profile
  425.  header("Location: ../html/login.html");
  426. }
  427. }
  428.  
  429. function test_input($data)
  430. {
  431.   $data = trim($data);
  432.   $data = stripslashes($data);
  433.   $data = htmlspecialchars($data);
  434.   return $data;
  435. }
  436.  
  437. ?>
  438.  
  439.    <!--Navigation bar-->
  440.  
  441.    <?php
  442. if (isset($_SESSION['user'])) {
  443.   require('member_nav.php');
  444. }
  445. else {
  446. require('guest_nav.php');
  447. }
  448. ?>
  449.  
  450. <br>
  451.  
  452.   <!--Banner-->
  453.  
  454.   <div class="banner">
  455.     <img src="../images/photoshare_banner.png" width="1099" alt="An image of the PhotoShare logo.">
  456.   </div>
  457.  
  458.   <br>
  459.  
  460.   <div id="reg_sign">
  461.     <h1 class="header_center">Sign Up Today!</h1>
  462.   </div>
  463.  
  464.   <br>
  465.  
  466.   <!--Registration form-->
  467.  
  468.   <div class="reg_form">
  469.     <form
  470.     id="register"
  471.     action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"
  472.     onsubmit="return validate();"
  473.     method="post">
  474.  
  475.       <fieldset>
  476.         <legend><strong>Account Information:</strong></legend>
  477.  
  478.         <input type="text" name="user_name" id="user_name" placeholder="Username" value="<?php echo $username; ?>">
  479.  
  480.         <span id="userErr" class="error"> <?php echo $userErr; ?> </span>
  481.  
  482.         <br><br>
  483.  
  484.         <input type="text" name="email" id="email" placeholder="Email" value="<?php echo $email; ?>">
  485.  
  486.         <span id="emailErr" class="error"><?php echo $emailErr; ?></span>
  487.  
  488.         <br><br>
  489.  
  490.         <input type="password" name="pword" id="pword" placeholder="Password" value="<?php echo $pword; ?>">
  491.  
  492.         <span id="pwordErr" class="error"> <?php echo $pwordErr; ?> </span>
  493.  
  494.         <br><br>
  495.  
  496.         <input type="password" name="vpass" id="vpass" placeholder="Verify Password">
  497.  
  498.       </fieldset>
  499.  
  500.       <br>
  501.  
  502.       <fieldset>
  503.         <legend><strong>Personal Information:</strong></legend>
  504.        
  505.         <input type="text" name="fname" id="fname" placeholder="First Name" value="<?php echo $fname; ?>">
  506.  
  507.         <span id="fnErr" class="error"> <?php echo $fnErr; ?> </span>
  508.  
  509.         <br><br>
  510.  
  511.         <input type="text" name="lname" id="lname" placeholder="Last Name" value="<?php echo $lname; ?>">
  512.  
  513.         <span id="lnErr" class="error"> <?php echo $lnErr; ?> </span>
  514.  
  515.         <br><br>
  516.  
  517.         <input type="text" name="age" id="age" placeholder="Age" value="<?php echo $age; ?>">
  518.  
  519.         <span id="ageErr" class="error"> <?php echo $ageErr; ?> </span>
  520.  
  521.         <br><br>
  522.  
  523.         <input type="text" name="phone" id="phone" placeholder="Phone Number" value="<?php echo $phone; ?>">
  524.  
  525.         <span id="phoneErr" class="error"> <?php echo $phoneErr; ?> </span>
  526.       </fieldset>
  527.  
  528.       <br>
  529.  
  530.       <input type="submit" name="submit" value="REGISTER">
  531.  
  532.     </form>
  533.   </div>
  534.  
  535.   <p class="form_para">Already have an account? Login in <a href="../html/login.html">here.</a></p>
  536.  
  537.   <?php
  538.  //footer
  539.  require('footer.php');
  540.  ?>
  541.  
  542. <!--javascript link for client side validation-->
  543. <script type="text/javascript" src="../javascript/myJS.js" ></script>
  544.  
  545. </body>
  546. </html>
  547.  
  548. //search.php
  549. <?php
  550.  
  551. //functioning search bar code comes from:
  552.  
  553. //YouTube. (2017). 57: How to create a search field with PHP and MySQLi | PHP tutorial | Learn PHP programming. YouTube. Retrieved December 16, 2023, from https://www.youtube.com/watch?v=lwgG_uIJYQM.
  554.  
  555.  
  556. require('session.php');
  557.  
  558. require('db_connect.php');
  559. ?>
  560.  
  561. <!--search results page that will display information from the post_info table in the database-->
  562.  
  563. <!DOCTYPE html>
  564. <html lang="en">
  565. <head>
  566.   <meta charset="UTF-8">
  567.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  568.   <title>Search Results</title>
  569.  
  570.   <link href="../css/style.css" rel="stylesheet" type="text/css">
  571.  
  572.   <link rel="icon" type="image/png" sizes="32x32" href="../images/favicon-32x32.png">
  573. </head>
  574. <body>
  575.  
  576. <?php
  577.   //navigation bar
  578. if (isset($_SESSION['user'])) {
  579.   require('member_nav.php');
  580. }
  581. else {
  582. require('guest_nav.php');
  583. }
  584. ?>
  585.  
  586.   <h1 class="header_center">Results</h1>
  587.  
  588.     <?php
  589.     if(isset($_POST["search_btn"])) {
  590.       $search = mysqli_real_escape_string($conn, $_POST["search"]);
  591.  
  592.       //query string, which includes the mysql wildcard
  593.       $qry = "select * from post_info where caption like '%$search%' or author like '%$search%';";
  594.  
  595.       $result = mysqli_query($conn, $qry);
  596.  
  597.       if($result) {
  598.         if (mysqli_num_rows($result) > 0) {
  599.           while ($row = mysqli_fetch_assoc($result)) {
  600.             //displaying search results
  601.             echo " <div class=\"gallery\">
  602.            <img src='$row[picture]'  alt='User Upload' width='500' height='333'>
  603.  
  604.            <br>
  605.  
  606.            <div class=\"caption\">$row[caption]</div>
  607.  
  608.            <div class=\"author\"><strong>$row[author]</strong></div>
  609.  
  610.            <div class=\"date\"><strong>$row[post_date]</strong></div>
  611.            
  612.            </div>";
  613.  
  614.           } //end while loop
  615.         } //end 3rd if statement
  616.         else
  617.           echo "No results found.";
  618.  
  619.       } //end 2nd if statement
  620.   mysqli_close($conn);
  621.  
  622.     } //end 1st if statement
  623.     ?>
  624. </body>
  625. </html>
  626.  
  627. //session.php
  628. <!--stores all the session information and will be included in all files that needs this info-->
  629. <?php
  630. session_start();
  631.  
  632. if (isset($_GET['logout'])) {
  633.     //code to close the session
  634.     session_unset();
  635.     // unset the session array i.e. destroy the data and the array
  636.     session_destroy();
  637.     // terminate the session.
  638.     header('Location: index.php');
  639.     //reload the index page.
  640. }
  641. ?>
  642.  
  643. //update_frm.php
  644. <?php
  645. require('session.php');
  646. ?>
  647.  
  648. <!--a member's only page, will display after users update their profile-->
  649.  
  650. <!DOCTYPE html>
  651. <html lang="en">
  652. <head>
  653.   <meta charset="UTF-8">
  654.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  655.   <title>Profile Updated</title>
  656.  
  657.   <link href="../css/style.css" rel="stylesheet" type="text/css">
  658.  
  659.   <link rel="icon" type="image/png" sizes="32x32" href="../images/favicon-32x32.png">
  660.  
  661. </head>
  662. <body>
  663.  
  664.   <?php
  665.   //navigation bar
  666.   require('member_nav.php');
  667.  
  668.   //connect to database
  669.   require('db_connect.php');
  670.  
  671.   //the user must enter their password to update the form
  672.   //edited to ensure the update won't go through if password isn't entered.
  673. if(empty($_POST["pword"])) {
  674.   die("Password must be entered to update. Go back to the <a href='edit_profile.php'>update</a> form.");
  675. }
  676.  
  677.   //initalizing variables
  678.   $username = $email = $pword = $fname = $lname = $age = $phone = "";
  679.  
  680.   //retrieving form data
  681.   $username = $_POST["user_name"];
  682.   $email = $_POST["email"];
  683.   $pword = $_POST["pword"];
  684.   $fname = $_POST["fname"];
  685.   $lname = $_POST["lname"];
  686.   $age = $_POST["age"];
  687.   $phone = $_POST["phone"];
  688.  
  689.  
  690. //to encrypt the password again
  691.   $pword_hash = hash('sha256', $pword);
  692.  
  693.   //Create the query string
  694.   $query = "update user_info set email = '$email', pword = '$pword_hash', fname = '$fname', lname = '$lname', age = '$age', phone_num = '$phone' where username = '$username';";  
  695.  
  696.   //execute the query
  697.   $result = mysqli_query($conn, $query);
  698.  
  699.   //process the results and provide feedback          
  700.   if ($result)
  701.   {
  702.    //query successfully executed in MySQL  
  703.    $rows_aff = mysqli_affected_rows($conn);
  704.    //retrieves the number of rows updated              
  705.    if ($rows_aff > 0) {// at least 1 row was updated                
  706.      echo "<h1 class=\"header_center\">Profile Information Updated</h1>
  707.    
  708.     $rows_aff rows were updated as requested. <br><br>";            
  709.    }
  710.    else
  711.     {
  712.      // query executed but no rows were updated                
  713.      echo "Sorry no rows were found for username: $username. <br><br>";          
  714.     }          
  715.     }
  716.     else {              
  717.      echo "Sorry, there was an error in processing this update. <br><br>";          
  718.  }          
  719.  
  720.     //close the connection        
  721.     mysqli_close($conn);
  722.   ?>  
  723. </body>
  724. </html>
  725.  
  726. //upload.php
  727. <?php
  728. require('session.php');
  729. ?>
  730.  
  731. <!--a member's only page, allows users to upload their pictures to the website and store it in the post_info table in the database-->
  732.  
  733. <!DOCTYPE html>
  734. <html lang="en">
  735. <head>
  736.   <meta charset="UTF-8">
  737.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  738.   <title>New Post</title>
  739.  
  740.   <link href="../css/style.css" rel="stylesheet" type="text/css">
  741.  
  742.   <link rel="icon" type="image/png" sizes="32x32" href="../images/favicon-32x32.png">
  743. </head>
  744. <body class="box">
  745.  
  746.   <!--Navigation bar-->
  747.  
  748.   <?php
  749. if (isset($_SESSION['user'])) {
  750.   require('member_nav.php');
  751. }
  752. else {
  753. require('guest_nav.php');
  754. }
  755. ?>
  756.  
  757. <br><br>
  758.  
  759. <?php
  760.  
  761. //database connection
  762. require('db_connect.php');
  763.  
  764. if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST["submit"])) {
  765.  
  766.  
  767.  
  768. //inputs from the form upload.html
  769. $caption = $_POST["caption"];
  770.  
  771. /*
  772. Code for the image upload comes from:
  773.   W3Schools. (n.d.). PHP File Upload. PHP file upload. https://www.w3schools.com/php/php_file_upload.asp
  774. Retrieved 21st Oct. 2023.*/
  775.  
  776.   //upload image
  777. $target_dir = "../user_images/";
  778. $target_file = $target_dir . basename($_FILES["user_upload"]["name"]);
  779. $uploadOk = 1;
  780. $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
  781.  
  782. // Check file size
  783. if ($_FILES["user_upload"]["size"] > 500000) {
  784.   echo "Sorry, your file is too large.";
  785.   $uploadOk = 0;
  786. }
  787.  
  788. // Allow certain file formats
  789. if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
  790. && $imageFileType != "gif" ) {
  791.   echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  792.   $uploadOk = 0;
  793. }
  794.  
  795. // Check if $uploadOk is set to 0 by an error
  796. if ($uploadOk == 0) {
  797.   echo "Sorry, your file was not uploaded.";
  798. // if everything is ok, try to upload file
  799. } else {
  800.   if (move_uploaded_file($_FILES["user_upload"]["tmp_name"], $target_file)) {
  801.     echo "The file ". htmlspecialchars( basename( $_FILES["user_upload"]["name"])). " has been uploaded.";
  802.   } else {
  803.     echo "Sorry, there was an error uploading your file.";
  804.   }
  805. }
  806.  
  807. //insert into database
  808. //use PHP variables as input to MqSQL query.
  809.  
  810. $qry = "INSERT INTO post_info (picture, caption, author) VALUES ('$target_file', '$caption', '$_SESSION[user]')";
  811.  
  812. //execute the query    
  813. $result = mysqli_query($conn, $qry);
  814.  
  815. //check on the success of the query
  816. if($result) echo 'record successfully inserted.<br><br>';
  817. else echo 'Error occurred: ' . mysqli_error($conn) . '<br><br>';
  818.  
  819. //close the connection
  820. mysqli_close($conn);
  821.  
  822. //after info is inserted into the database, the user will be directed to the gallery page to view their post
  823. header("Location: gallery.php");
  824.  
  825. }
  826. ?>
  827.  
  828. <form id="postfrm" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" enctype="multipart/form-data">
  829.       <fieldset>
  830.         <legend><strong>Upload Picture</strong></legend>
  831.  
  832.         <input type="file" name="user_upload" id="user_upload" required>
  833.       </fieldset>
  834.  
  835.       <fieldset>
  836.         <legend><strong>Post Information</strong></legend>
  837.  
  838.         <textarea name="caption" rows="5" cols="30" placeholder="Type your caption here" maxlength="300"></textarea>
  839.       </fieldset>
  840.  
  841.       <br><br>
  842.  
  843.       <input type="submit" name="submit" value="POST">
  844.     </form>
  845.  
  846.     <?php
  847.  //footer
  848.  require('footer.php');
  849.  ?>
  850.  
  851. </body>
  852. </html>
  853.  
  854. //verify_login.php
  855. <?php
  856. //session info
  857. require('session.php');
  858. ?>
  859.  
  860. <!--login authentication to ensure the data the user uses to login is the same as the one stored in the database-->
  861.  
  862. <!DOCTYPE html>
  863. <html lang="en">
  864. <head>
  865.   <meta charset="UTF-8">
  866.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  867.   <title>Welcome Back!</title>
  868.   <link href="../css/style.css" rel="stylesheet" type="text/css">
  869.  
  870.   <link rel="icon" type="image/png" sizes="32x32" href="../images/favicon-32x32.png">
  871. </head>
  872. <body>
  873.   <?php
  874.  
  875.   $username = $pword = $db_pass = "";
  876.  
  877.   if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST["submit"])) {
  878.  
  879.     //storing form data into php variables
  880.     $username = $_POST["user_name"];
  881.     $pword = $_POST["password"];
  882.  
  883.     //database connection
  884.     require("db_connect.php");
  885.  
  886.     //Create the query string
  887.     $query = "select * from user_info where username = '$username';";
  888.  
  889.     //execute the query
  890.     $result = mysqli_query($conn, $query);
  891.     if ($result) {
  892.  
  893.         if (mysqli_num_rows($result) > 0) {
  894.  
  895.             while ($row = mysqli_fetch_assoc($result)) {
  896.    
  897.  
  898.                 //storing database password into a php variable
  899.         $db_pass = $row["pword"];
  900.  
  901.             }
  902.             //end while loop
  903.  
  904.         }
  905.         //end if 1 or more rows
  906.  
  907.         else {
  908.             echo "<br>Incorrect Username.";
  909.     }
  910.  
  911. }
  912.  
  913.   //encrypting password from login form
  914.   $pword_hash = hash('sha256', $pword);
  915.  
  916.   //verifying password and setting up session data
  917.  
  918.   if($pword_hash != $db_pass) {
  919.   echo "<h3>Incorrect password.</h3>";
  920. }
  921. else {
  922.    $_SESSION['user'] = $username;
  923.   }
  924.  
  925.   mysqli_close($conn);
  926.  
  927.   }
  928.   ?>
  929.  
  930.    <!--Navigation bar-->
  931.  
  932.    <?php
  933. if (isset($_SESSION['user'])) {
  934.   require('member_nav.php');
  935. }
  936. else {
  937. require('guest_nav.php');
  938. }
  939. ?>
  940.  
  941. <br>
  942.  
  943.   <?php
  944.   if(isset($_SESSION['user'])) {
  945.     echo "<h1 class=\"header_center\">Welcome Back!</h1>
  946.    <br><br>
  947.    <p>You can create a post <a href='upload.php'>here</a> or check out other users posts in the <a href='gallery.php'>Gallery</a>.</p>";
  948.   }
  949.   else {
  950.     echo "<h3>You need to <a href='../html/login.html'>login</a> again.</h3>";
  951.   }
  952.  
  953.   //footer
  954.  require('footer.php');
  955.   ?>
  956. </body>
  957. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement