Loading:


Skrypt logowania [php]

Poniższy kod demonstruje jak zaimplementować kod logowania, bazując na cookies, używając kodowania md5. Skrypt nie jest mój.

Źródło: http://www.ceskiuzzisoft.cjb.net

Oto kod:



Napisz Artykuł

Listing

<?php
/***************************************************************************
 *                         Login
 *                    ------------------------------
 *   Filename:             login.php
 *   Project:              Uzzisoft - PHP Simple Member System
 *   By:                   Alejandro(Alex) Iannuzzi
 *   Company:              Uzzi Soft
 *   Copyright:            (C) 2004 Uzzi Soft - Ceski Iannuzzi
 *   Website:              http://www.ceskiuzzisoft.cjb.net
 *   Email:                ceski02@hotmail.com
 *   License:              Freeware
 *   Version:              1.0.2.1
 *   Build Date:           2004-05-10
 *
 *   If you find bugs/errors/anything else you would like to point to out
 *   to us please feel free to contact us.
 *
 *   What it does:
 *   Allows the user to log into the system
 *
 *   How it does it:
 *   Checks if there is a cookie present. Gets username and password that
 *   came from login.html file. Checks if they are empty. MD5's the username
 *   and password. [We can't use connect.php right now since there is no
 *   cookie at all] It then validates the user. We get the MySQL time
 *   and make a new cookie.
 *
 *   Check if there is a cookie now. If there is verify the user. [Sometimes
 *   there will be no cookie created straight away so we have to make it so
 *   the user can log in with a cookie or no cookie in this page]
 *   Get the users information and print that out to a table.
 *
 *   Check how many people are online right now. Check if user is on the
 *   list. If they are not then add them to the users that are online and
 *   print the users that are online. If user is alreadly on list then
 *   just print the list.
 *
 ***************************************************************************/


  // If there is no cookie presesnt
  if (!isset($_COOKIE['cookie_info'])) {

      // Variables that data come from the form
      $username = $_POST["user"];
      $password = $_POST["pass"];

      // Check if username and password where submitted
      if (!$username) {
          echo "Please enter username"; exit;
      }
      if (!$password) {
          echo "Please enter password"; exit;
      }

      // Use Connect Script
      include("connect.php");

      // MD5 Username and Password
      $username = MD5($username);
      $password = MD5($password);

      // Check if username exists. If not then say no such username.
      $issuchusername = mysql_query("SELECT * FROM userdata WHERE userid = '$username'");
      $usernamelogin = mysql_num_rows($issuchusername);

      // If username exists
      if ($usernamelogin == 1) {

          $issuchpassword = mysql_query("SELECT * FROM userdata WHERE userid = '$username' AND userpass = '$password'");
          $passwordlogin = mysql_num_rows($issuchpassword);

          // If password is correct
          if ($passwordlogin == 1) {

              $time = time();
              $cookie_data = $username.'-'.$password;
              if(setcookie ("cookie_info",$cookie_data, $time+3600)==TRUE) {
              }
              else {
                  echo "You computer does not support cookies. <BR> To view other pages after logged in you need to have cookies enabled.<BR>";
              }

          }
          else {
              echo "Incorrect username/password";
              exit;
          }
      }
      else {
          echo "Incorrect username/password";
          exit;
      }
  }
  // End if no cookie present

?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Login</title>

<script language="JavaScript">
<!--
function FP_swapImg() {//v1.0
 var doc=document,args=arguments,elm,n; doc.$imgSwaps=new Array(); for(n=2; n<args.length;
 n+=2) { elm=FP_getObjectByID(args[n]); if(elm) { doc.$imgSwaps[doc.$imgSwaps.length]=elm;
 elm.$src=elm.src; elm.src=args[n+1]; } }
}

function FP_preloadImgs() {//v1.0
 var d=document,a=arguments; if(!d.FP_imgs) d.FP_imgs=new Array();
 for(var i=0; i<a.length; i++) { d.FP_imgs[i]=new Image; d.FP_imgs[i].src=a[i]; }
}

function FP_getObjectByID(id,o) {//v1.0
 var c,el,els,f,m,n; if(!o)o=document; if(o.getElementById) el=o.getElementById(id);
 else if(o.layers) c=o.layers; else if(o.all) el=o.all[id]; if(el) return el;
 if(o.id==id || o.name==id) return o; if(o.childNodes) c=o.childNodes; if(c)
 for(n=0; n<c.length; n++) { el=FP_getObjectByID(id,c[n]); if(el) return el; }
 f=o.forms; if(f) for(n=0; n<f.length; n++) { els=f[n].elements;
 for(m=0; m<els.length; m++){ el=FP_getObjectByID(id,els[n]); if(el) return el; } }
 return null;
}
// -->
</script>
</head>

<body onload="FP_preloadImgs(/*url*/'buttonA.jpg',/*url*/'button9.jpg',/*url*/'buttonD.jpg',/*url*/'buttonC.jpg')">

<a href="login.html">
<img border="0" id="img1" src="button8.jpg" height="20" width="100" alt="Login" onmouseover="FP_swapImg(1,0,/*id*/'img1',/*url*/'button9.jpg')" onmouseout="FP_swapImg(0,0,/*id*/'img1',/*url*/'button8.jpg')" onmousedown="FP_swapImg(1,0,/*id*/'img1',/*url*/'buttonA.jpg')" onmouseup="FP_swapImg(0,0,/*id*/'img1',/*url*/'button9.jpg')" fp-style="fp-btn: Glass Rectangle 1; fp-orig: 0" fp-title="Login"></a>&nbsp;
<a href="createuser.html">
<img border="0" id="img2" src="buttonB.jpg" height="20" width="127" alt="Create New User" onmouseover="FP_swapImg(1,0,/*id*/'img2',/*url*/'buttonC.jpg')" onmouseout="FP_swapImg(0,0,/*id*/'img2',/*url*/'buttonB.jpg')" onmousedown="FP_swapImg(1,0,/*id*/'img2',/*url*/'buttonD.jpg')" onmouseup="FP_swapImg(0,0,/*id*/'img2',/*url*/'buttonC.jpg')" fp-style="fp-btn: Glass Rectangle 1; fp-proportional: 0; fp-orig: 0" fp-title="Create New User"></a><p><br>
<p>

<?php

  // Use Connect Script
  include("connect.php");

  // Use cookie and Extract the cookie data (Username and Password)
  $cookie_info = explode("-", $_COOKIE['cookie_info']);
  $namecookie = $cookie_info[0];
  $passcookie = $cookie_info[1];

  if (!isset($_COOKIE['cookie_info'])) {
      $namecookie = $_POST["user"];
      $passcookie = $_POST["pass"];

      // MD5 Username and Password
      $namecookie = MD5($namecookie);
      $passcookie = MD5($passcookie);
  }

  // Check if username exists. If not then say no such username.
  $issuchusername = mysql_query("SELECT * FROM userdata WHERE userid = '$namecookie'");
  $usernamelogin = mysql_num_rows($issuchusername);

  // If username exists
  if ($usernamelogin == 1) {
      $issuchpassword = mysql_query("SELECT * FROM userdata WHERE userid = '$namecookie' AND userpass = '$passcookie'");
      $passwordlogin = mysql_num_rows($issuchpassword);

      // If password is correct
      if ($passwordlogin == 1) {

          // User is now logged in, display details of user

          // Get details of user from Database and put them in variables
          $query = mysql_query("SELECT * FROM userdata WHERE userid = '$namecookie'");
          $nameuser = mysql_result($query,0,13);
          $name = mysql_result($query,0,2);
          $address = mysql_result($query,0,3);
          $suburb = mysql_result($query,0,4);
          $state = mysql_result($query,0,5);
          $postcode = mysql_result($query,0,6);
          $phone1 = mysql_result($query,0,7);
          $phone2 = mysql_result($query,0,8);
          $emailaddress = mysql_result($query,0,9);
          $joineddate = mysql_result($query,0,10);

          echo "<B>Welcome $nameuser <BR>You are now Logged in</B><BR><BR>";

?>

<form action="updateuser.php" method="POST">

<?

          // Make the table displaying details of user and a update and reset button
          echo "
<table border='0' width='100%' id='table1' height='300'>
        <tr>
                <td width='115'><font face='Tahoma'>Username:</td>
                <td> <font face='Tahoma'>$nameuser </font></td>
        </tr>
        <tr>
                <td width='115'><font face='Tahoma'>Name:</td>
                <td> <font face='Tahoma'>$name </font></td>
        </tr>
        <tr>
                <td width='115'><font face='Tahoma'>Address:</td>
                <td> <font face='Tahoma'>$address </font></td>
        </tr>
        <tr>
                <td width='115'><font face='Tahoma'>Suburb/City:</td>
                <td> <font face='Tahoma'>$suburb </font></td>
        </tr>
        <tr>
                <td width='115'><font face='Tahoma'>State:</td>
                <td> <font face='Tahoma'>$state </font></td>
        </tr>
        <tr>
                <td width='115'><font face='Tahoma'>Postcode:</td>
                <td> <font face='Tahoma'>$postcode </font></td>
        </tr>
        <tr>
                <td width='115'><font face='Tahoma'>Phone #1:</td>
                <td> <font face='Tahoma'>$phone1 </font></td>
        </tr>
        <tr>
                <td width='115'><font face='Tahoma'>Phone #2:</td>
                <td> <font face='Tahoma'>$phone2 </font></td>
        </tr>
        <tr>
                <td width='115'><font face='Tahoma'>Email Address:</td>
                <td> <font face='Tahoma'>$emailaddress </font> </td>
        </tr>
        <tr>
                <td width='115'><font face='Tahoma'>Joined Date:</font></td>
                <td> <font face='Tahoma'>$joineddate </font></td>
        </tr>
</table>"
;


?>

</form>
<a href="login.php">
<img border="0" id="img6" src="button2.jpg" height="20" width="100" alt="Login Home" onmouseover="FP_swapImg(1,0,/*id*/'img6',/*url*/'button3.jpg')" onmouseout="FP_swapImg(0,0,/*id*/'img6',/*url*/'button2.jpg')" onmousedown="FP_swapImg(1,0,/*id*/'img6',/*url*/'button4.jpg')" onmouseup="FP_swapImg(0,0,/*id*/'img6',/*url*/'button3.jpg')" fp-style="fp-btn: Soft Capsule 5" fp-title="Login Home"></a>
<a href="updateuser.php">
<img border="0" id="img3" src="button18.jpg" height="20" width="122" alt="Update my Details" onmouseover="FP_swapImg(1,0,/*id*/'img3',/*url*/'button19.jpg')" onmouseout="FP_swapImg(0,0,/*id*/'img3',/*url*/'button18.jpg')" onmousedown="FP_swapImg(1,0,/*id*/'img3',/*url*/'button1A.jpg')" onmouseup="FP_swapImg(0,0,/*id*/'img3',/*url*/'button19.jpg')" fp-style="fp-btn: Soft Capsule 5; fp-proportional: 0" fp-title="Update my Details"></a>
<a href="changepassword.php">
<img border="0" id="img4" src="button1C.jpg" height="20" width="142" alt="Change my Password" onmouseover="FP_swapImg(1,0,/*id*/'img4',/*url*/'button1D.jpg')" onmouseout="FP_swapImg(0,0,/*id*/'img4',/*url*/'button1C.jpg')" onmousedown="FP_swapImg(1,0,/*id*/'img4',/*url*/'button1E.jpg')" onmouseup="FP_swapImg(0,0,/*id*/'img4',/*url*/'button1D.jpg')" fp-style="fp-btn: Soft Capsule 5; fp-proportional: 0" fp-title="Change my Password"></a>
<a href="logout.php">
<img border="0" id="img5" src="button1F.jpg" height="20" width="100" alt="Logout" onmouseover="FP_swapImg(1,0,/*id*/'img5',/*url*/'button20.jpg')" onmouseout="FP_swapImg(0,0,/*id*/'img5',/*url*/'button1F.jpg')" onmousedown="FP_swapImg(1,0,/*id*/'img5',/*url*/'button21.jpg')" onmouseup="FP_swapImg(0,0,/*id*/'img5',/*url*/'button20.jpg')" fp-style="fp-btn: Soft Capsule 5; fp-proportional: 0" fp-title="Logout"></a><p><br>

<?php

          // Check how many people are online
          // Grab the number of people online now
          $numberofpeopleonlinecount = mysql_query("SELECT * FROM usersonline");
          $numberofpplonline = mysql_num_rows($numberofpeopleonlinecount);

          // Check if user is on online list
          $isthisuseronline = mysql_query("SELECT * FROM usersonline WHERE username = '$nameuser'");
          $isuseronline = mysql_num_rows($isthisuseronline);

          // If user is not on list then add them
          if ($isuseronline == 0) {
              echo "Adding User to online user list<BR>";
              mysql_query("INSERT INTO usersonline VALUES ('$nameuser')");

              // Print the number of users online
              $numberofpeopleonlinecount = mysql_query("SELECT * FROM usersonline");
              $numberofpplonline = mysql_num_rows($numberofpeopleonlinecount);

              echo "Current Users Online: ";
              for ($i = 0; $i < $numberofpplonline; $i++) {
                  $isonlinequery = mysql_query("SELECT * FROM usersonline");
                  $useronline = mysql_result($isonlinequery,$i,0);
                  // Print current username gotten from i
                  echo "$useronline ";
              }
          }

          // Get all users online
          else {
              // Print the number of users online
              echo "Current Users Online: ";
              for ($i = 0; $i < $numberofpplonline; $i++) {
                  $isonlinequery = mysql_query("SELECT * FROM usersonline");
                  $useronline = mysql_result($isonlinequery,$i,0);
                  // Print current username gotten from i
                  echo "$useronline ";
              }
          }

          // Print the number of users online
          echo "<BR> There are $numberofpplonline Users Online <BR>";

      // If user and password are not correct print error message
      }
      else {
          echo "Incorrect username/password";
          exit;
      }
  }
  else {
      echo "Incorrect username/password";
      exit;
  }
  // End Login

?>

</body>
</html>


Ten wpis posiada swój wątek na forum

Wszystkie pytania prosimy kierować właśnie tam ponieważ komentarze to miejsce na poprawki do kodu lub alternatywne rozwiązania i pytania nie będą publikowane

http://forum.funkcje.net/forum/viewtopic.php?f=2&t=5887

Dane do logowania na forum są takie same jak na funkcje.net



Dodano przez: ceski Ranga: Poziom 9 Punktów: 399
Komentarze użytkowników
    • Treść komentarza
      Kod do komentarza (opcjonalnie)
      PHP JavaScript MySQL Smarty SQL HTML CSS ActionScript
      Autor
      Token
      token

       

       





funkcje.net
Wszelkie prawa zastrzeżone©. | Funkcje.net 2008-13 v.1.5 | design: diviXdesign & rainbowcolors