Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. 1) Δημιουργήστε το PHP αρχείο: index.php
  2. Εισάγετε στο αρχείο τον παρακάτω κώδικα ώστε να δημιουργηθεί μια HTML login form.
  3. <?php
  4. include('login.php'); // Includes Login Script
  5. if(isset($_SESSION['login_user'])){
  6. header("location: profile.php");
  7. }
  8. ?>
  9. <!DOCTYPE html>
  10. <html>
  11. <head>
  12. <title>Login Form με PHP και Session</title>
  13. <link href="style.css" rel="stylesheet" type="text/css">
  14. </head>
  15. <body>
  16. <div id="main">
  17. <h1>PHP Login Session Example</h1>
  18. <div id="login">
  19. <h2>Login Form</h2>
  20. <form action="" method="post">
  21. <label>UserName :</label>
  22. <input id="name" name="username" placeholder="username" type="text">
  23. <label>Password :</label>
  24. <input id="password" name="password" placeholder="**********" type="password">
  25. <input name="submit" type="submit" value=" Login ">
  26. <span><?php echo $error; ?></span>
  27. </form>
  28. </div>
  29. </div>
  30. </body>
  31. </html>
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40. 2) Δημιουργήστε αρχείο PHP File: login.php
  41. Θα περιέχει ένα login script στο οποίο θα ξεκινά το PHP session:
  42. <?php
  43. session_start(); // Starting Session
  44. $error=''; // Variable To Store Error Message
  45. if (isset($_POST['submit'])) {
  46. if (empty($_POST['username']) || empty($_POST['password'])) {
  47. $error = "Username or Password is invalid";
  48. }
  49. else
  50. {
  51. // Define $username and $password
  52. $username=$_POST['username'];
  53. $password=$_POST['password'];
  54. // Establishing Connection with Server by passing server_name, user_id and password as a parameter
  55. $connection = mysql_connect("localhost", "root", "");
  56. // To protect MySQL injection for Security purpose
  57. $username = stripslashes($username);
  58. $password = stripslashes($password);
  59. $username = mysql_real_escape_string($username);
  60. $password = mysql_real_escape_string($password);
  61. // Selecting Database
  62. $db = mysql_select_db("company", $connection);
  63. // SQL query to fetch information of registerd users and finds user match.
  64. $query = mysql_query("select * from login where password='$password' AND username='$username'", $connection);
  65. $rows = mysql_num_rows($query);
  66. if ($rows == 1) {
  67. $_SESSION['login_user']=$username; // Initializing Session
  68. header("location: profile.php"); // Redirecting To Other Page
  69. } else {
  70. $error = "Username or Password is invalid";
  71. }
  72. mysql_close($connection); // Closing Connection
  73. }
  74. }
  75. ?>
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87. 3) Δημιουργήστε το αρχείο PHP File: profile.php
  88. Είναι η σελίδα redirected μετά από επιτυχημένο login.
  89.  
  90. <?php
  91. include('session.php');
  92. ?>
  93. <!DOCTYPE html>
  94. <html>
  95. <head>
  96. <title>Your Home Page</title>
  97. <link href="style.css" rel="stylesheet" type="text/css">
  98. </head>
  99. <body>
  100. <div id="profile">
  101. <b id="welcome">Welcome : <i><?php echo $login_session; ?></i></b>
  102. <b id="logout"><a href="logout.php">Log Out</a></b>
  103. </div>
  104. </body>
  105. </html>
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113. 4) Δημιουργήστε το αρχείο PHP File: session.php
  114. Αυτή η σελίδα επιστρέφει πληροφορίες για τον χρήστη που είναι logged in.
  115.  
  116. <?php
  117. // Establishing Connection with Server by passing server_name, user_id and password as a parameter
  118. $connection = mysql_connect("localhost", "root", "");
  119. // Selecting Database
  120. $db = mysql_select_db("company", $connection);
  121. session_start();// Starting Session
  122. // Storing Session
  123. $user_check=$_SESSION['login_user'];
  124. // SQL Query To Fetch Complete Information Of User
  125. $ses_sql=mysql_query("select username from login where username='$user_check'", $connection);
  126. $row = mysql_fetch_assoc($ses_sql);
  127. $login_session =$row['username'];
  128. if(!isset($login_session)){
  129. mysql_close($connection); // Closing Connection
  130. header('Location: index.php'); // Redirecting To Home Page
  131. }
  132. ?>
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140. 5) Δημιουργήστε ένα αρχείο PHP File: logout.php
  141. To αρχείο αυτό καταργεί όλες τις sessions και ανακατευθύνει στο home page.
  142.  
  143. <?php
  144. session_start();
  145. if(session_destroy()) // Destroying All Sessions
  146. {
  147. header("Location: index.php"); // Redirecting To Home Page
  148. }
  149. ?>
  150.  
  151.  
  152.  
  153.  
  154. 6) Δημιουργήστε τα απαραίτητα στοιχεία της βάσης
  155.  
  156. Για την δημιουργία της database και του table, εκτελέστε τον ακόλουθο κώδικα SQL .
  157.  
  158. CREATE DATABASE company;
  159. CREATE TABLE login(
  160. id int(10) NOT NULL AUTO_INCREMENT,
  161. username varchar(255) NOT NULL,
  162. password varchar(255) NOT NULL,
  163. PRIMARY KEY (id)
  164. )
  165.  
  166.  
  167.  
  168.  
  169.  
  170. 7) Δημιουργήστε το CSS File: style.css
  171.  
  172. Εισάγετε τα ακόλουθα HTML elements:
  173.  
  174. @import http://fonts.googleapis.com/css?family=Raleway;
  175. /*----------------------------------------------
  176. CSS Settings For HTML Div ExactCenter
  177. ------------------------------------------------*/
  178. #main {
  179. width:960px;
  180. margin:50px auto;
  181. font-family:raleway
  182. }
  183. span {
  184. color:red
  185. }
  186. h2 {
  187. background-color:#FEFFED;
  188. text-align:center;
  189. border-radius:10px 10px 0 0;
  190. margin:-10px -40px;
  191. padding:15px
  192. }
  193. hr {
  194. border:0;
  195. border-bottom:1px solid #ccc;
  196. margin:10px -40px;
  197. margin-bottom:30px
  198. }
  199. #login {
  200. width:300px;
  201. float:left;
  202. border-radius:10px;
  203. font-family:raleway;
  204. border:2px solid #ccc;
  205. padding:10px 40px 25px;
  206. margin-top:70px
  207. }
  208. input[type=text],input[type=password] {
  209. width:99.5%;
  210. padding:10px;
  211. margin-top:8px;
  212. border:1px solid #ccc;
  213. padding-left:5px;
  214. font-size:16px;
  215. font-family:raleway
  216. }
  217. input[type=submit] {
  218. width:100%;
  219. background-color:#FFBC00;
  220. color:#fff;
  221. border:2px solid #FFCB00;
  222. padding:10px;
  223. font-size:20px;
  224. cursor:pointer;
  225. border-radius:5px;
  226. margin-bottom:15px
  227. }
  228. #profile {
  229. padding:50px;
  230. border:1px dashed grey;
  231. font-size:20px;
  232. background-color:#DCE6F7
  233. }
  234. #logout {
  235. float:right;
  236. padding:5px;
  237. border:dashed 1px gray
  238. }
  239. a {
  240. text-decoration:none;
  241. color:#6495ed
  242. }
  243. i {
  244. color:#6495ed
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement