Advertisement
themandotexe

Untitled

Apr 6th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.02 KB | None | 0 0
  1. <--config.php-->
  2. <?php
  3. define('DB_SERVER', 'sampleserver');
  4. define('DB_USERNAME', 'sampleuser');
  5. define('DB_PASSWORD', 'samplepassword');
  6. define('DB_DATABASE', 'sampledb');
  7. $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
  8. ?>
  9. <-- login.php -->
  10. <?php
  11. include("config.php");
  12. session_start();
  13.  
  14. if($_SERVER["REQUEST_METHOD"] == "POST") {
  15. // username and password sent from form
  16.  
  17. $myusername = mysqli_real_escape_string($db,$_POST['username']);
  18. $mypassword = mysqli_real_escape_string($db,$_POST['password']);
  19.  
  20. $sql = "SELECT * FROM Student_Record WHERE username = '$myusername' and password = '$mypassword'";
  21. $result = mysqli_query($db,$sql);
  22. $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
  23.  
  24. $count = mysqli_num_rows($result);
  25.  
  26. // If result matched $myusername and $mypassword, table row must be 1 row
  27.  
  28. if($count == 1) {
  29. session_register("myusername");
  30. $_SESSION['login_user'] = $myusername;
  31. header("location: welcome.php");
  32. }else {
  33. $error = "Your Login Name or Password is invalid";
  34. }
  35. }
  36. ?>
  37. <html>
  38.  
  39. <head>
  40. <title>Login Page</title>
  41.  
  42. <style type = "text/css">
  43. body {
  44. font-family:Arial, Helvetica, sans-serif;
  45. font-size:14px;
  46. }
  47.  
  48. label {
  49. font-weight:bold;
  50. width:100px;
  51. font-size:14px;
  52. }
  53.  
  54. .box {
  55. border:#666666 solid 1px;
  56. }
  57. </style>
  58.  
  59. </head>
  60.  
  61. <body bgcolor = "#FFFFFF">
  62.  
  63. <div align = "center">
  64. <div style = "width:300px; border: solid 1px #333333; " align = "left">
  65. <div style = "background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div>
  66.  
  67. <div style = "margin:30px">
  68.  
  69. <form action = "" method = "post">
  70. <label>Username:</label><input type = "text" name = "username" class = "box"/><br /><br />
  71. <label>Password:</label><input type = "password" name = "password" class = "box" /><br/><br />
  72. <input type = "submit" value = "Submit"/><br />
  73. </form>
  74.  
  75. <div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div>
  76. </div>
  77. </div>
  78. </div>
  79. </body>
  80. </html>
  81.  
  82. function validate() {
  83. var studentid = document.getElementById("studentid").value;
  84. var name = document.getElementById("name").value;
  85. var email = document.getElementById("email").value;
  86. if (nameEmpty(name)) {
  87. if (studentidEmpty(studentid)) {
  88. if (emailEmpty(email)) {
  89. if (digitCheck(studentid)) {
  90. if (checkEmail(email)) {
  91. return verify(name, studentid);
  92. }
  93. }
  94. }
  95. }
  96. }
  97. return false;
  98. }
  99.  
  100. function studentidEmpty(studentid) {
  101. if (studentid == "") {
  102. alert("Please provide your student id!");
  103. document.getElementById("studentid").focus();
  104. return false;
  105. } else {
  106. return true;
  107. }
  108. }
  109.  
  110. <-- studentform.js -->
  111. function nameEmpty(name) {
  112. if (name == "") {
  113. alert("Please provide your name!");
  114. document.getElementById("name").focus();
  115. return false;
  116. } else {
  117. return true;
  118. }
  119. }
  120.  
  121. function emailEmpty(email) {
  122. if (email == "") {
  123. alert("Please provide your email!");
  124. document.getElementById("email").focus();
  125. return false;
  126. } else {
  127. return true;
  128. }
  129. }
  130.  
  131. function digitCheck(studentid) {
  132. var ok = studentid.search(".{8,}");
  133. if (ok != 0) {
  134. alert("Please provide ID with 8 digits.");
  135. return false;
  136. } else {
  137. return true;
  138. }
  139. }
  140.  
  141. function checkEmail(email) {
  142. var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  143. if (!filter.test(email)) {
  144. alert('Please provide a valid email address');
  145. email.focus;
  146. return false;
  147. } else {
  148. return true;
  149. }
  150. }
  151.  
  152.  
  153. <-- welcome.php -->
  154. <?php
  155. include('session.php');
  156. ?>
  157. <html lang="en">
  158. <head>
  159. <meta charset="utf-8">
  160. <title>Student Login Form</title>
  161. <link rel='stylesheet' href='studentform.css' type='text/css'/>
  162. <script src="studentform.js"></script>
  163. </head>
  164. <body>
  165. <h1>Student Login</h1>
  166. <div class="container">
  167. <form name="form" onsubmit="return validate();" action="javascript:void(0)">
  168. <label for="name">Name:</label>
  169. <input type="text" name="name" size="50" id="name" required />
  170.  
  171. <label for="studentid">Student ID:</label>
  172. <input type="number" name="studentid" maxlength="8" id="studentid" required />
  173.  
  174. <label for="email">Email:</label>
  175. <input type="email" name="email" size="50" id="email" required />
  176.  
  177. <label for="emailconfirm">Email Confirmation:</label>
  178. <input type="checkbox" name="emailconfirm" checked /><span>Send an email confirmation</span>
  179.  
  180. <label for="course">Course (if you want to select Student Registration):</label>
  181. <input type="text" name="course" size="50" id="course"/>
  182.  
  183. <label for="change">Type Add or Drop (if you want to select Student Registration):</label>
  184. <input type="text" name="change" size="50" id="change"/>
  185.  
  186. <select name="Options">
  187. <option value="Register">Student Registration</option>
  188. <option value="Transcript">Transcript</option>
  189. </select>
  190.  
  191. <input type="submit" name="submit" value="Submit" />
  192. </form>
  193. </div>
  194. </body>
  195. </html>
  196.  
  197. <-- session.php -->
  198. <?php
  199. include('config.php');
  200. session_start();
  201.  
  202. $user_check = $_SESSION['login_user'];
  203.  
  204. $ses_sql = mysqli_query($db,"SELECT username from Student_Record where username = '$user_check'");
  205.  
  206. $row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);
  207.  
  208. $login_session = $row['username'];
  209.  
  210. if(!isset($_SESSION['login_user'])){
  211. header("location:login.php");
  212. }
  213.  
  214. if(isset($_POST['form']))
  215. {
  216. $Options = $_POST['Options'];
  217. }
  218.  
  219. switch($Options)
  220. {
  221. case "Transcipt":{
  222. $name = $_POST['name'];
  223. $sql = "SELECT * from Student_Record where fullname = '$name'";
  224. //echo $sql;
  225. $result=$db->query($sql);
  226. if ($result->num_rows > 0) {
  227. $query = "SELECT * from Student_Record where fullname = '$name'"; //You don't need a ; like you do in SQL
  228. //$result1 = mysqli_query($conn,$query);
  229. $result1=$conn->query($query);
  230. echo "<br><br><table>"; // start a table tag in the HTML
  231. while($row = $result1->fetch_assoc()){ //Creates a loop to loop through results
  232. echo "<tr><td>" . $row['fullname'] . "</td><td>" . $row['Student_ID'] . "</td><td>" . $row['email'] . "</td><td>" . $row['courses'] . "</td><td>" . $row['grades'] . "</td></tr>"; //$row['index'] the index here is a field name
  233. }
  234. echo "</table>"; //Close the table in HTML
  235. }
  236. else{
  237. echo "not found";
  238. }
  239. }
  240.  
  241. case "Register":
  242. {
  243. $selection = $_POST['change'];
  244. $course = $_POST['course'];
  245. $name = $_POST['name'];
  246. $sql = "SELECT courses from Student_Record where fullname = '$name'";
  247. $result = $db->query($sql);
  248. if($selection="Add"){
  249. $newcourses = $result + $course
  250. $sql = "UPDATE MyGuests SET courses='$newcourses' WHERE fullname='$name'";
  251.  
  252. if (mysqli_query($conn, $sql)) {
  253. echo "Record updated successfully";
  254. } else {
  255. echo "Error updating record: " . mysqli_error($conn);
  256. }
  257. }
  258. if($selection="Drop"){
  259. $newcourses = str_replace($course,"",$result);
  260. $sql = "UPDATE Student_Record SET courses='$newcourses' WHERE fullname='$name'";
  261.  
  262. if (mysqli_query($conn, $sql)) {
  263. echo "Record updated successfully";
  264. } else {
  265. echo "Error updating record: " . mysqli_error($conn);
  266. }
  267. }
  268. if ($result->num_rows > 0) {
  269. $query = "SELECT * from Student_Record where fullname = '$name'"; //You don't need a ; like you do in SQL
  270. //$result1 = mysqli_query($conn,$query);
  271. $result1=$conn->query($query);
  272. echo "<br><br><table>"; // start a table tag in the HTML
  273. while($row = $result1->fetch_assoc()){ //Creates a loop to loop through results
  274. echo "<tr><td>" . $row['fullname'] . "</td><td>" . $row['Student_ID'] . "</td><td>" . $row['email'] . "</td><td>" . $row['courses'] . "</td><td>" . $row['grades'] . "</td></tr>"; //$row['index'] the index here is a field name
  275. }
  276. echo "</table>"; //Close the table in HTML
  277. }
  278. else{
  279. echo "not found";
  280. }
  281. break;
  282. }
  283. default: {
  284. echo("Error!");
  285. exit();
  286. break;
  287. }
  288. }
  289.  
  290. $checkQuery = "SELECT * from Student_Record WHERE username='$_POST[fullname]'";
  291. $userCheck = mysqli_query($db, $checkQuery);
  292. if(!$userCheck){
  293. echo "Invalid name";
  294. return false;
  295. }
  296.  
  297. $checkQuery = "SELECT * from Student_Record WHERE Student_ID='$_POST[studentid]'";
  298. $userCheck = mysqli_query($db, $checkQuery);
  299. if(!$userCheck){
  300. echo "Invalid Student ID";
  301. return false;
  302. }
  303. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement