Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Logowanie na sesjach
Forum PHP.pl > Forum > PHP
Ardziej
index.php

  1. <?php session_start(); ?>
  2. <html>
  3. <head>
  4. <title>Proste Logowanie</title>
  5. <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  6. </head>
  7. <body>
  8. <?php
  9. if(isSet($_SESSION['user']))
  10. {
  11.  echo "<b>Witaj: </b>".$_SESSION['user'];
  12.  echo "<a href=\"logout.php\">Wyloguj się</a>";
  13. }
  14. else{
  15. ?>
  16. <form method='POST' action='sprawdz.php'>
  17. <table>
  18. <tr><th align="right">Login:</th><td align="left"><input type='text' name='login'></td></tr>
  19. <tr><th align="right">Hasło:</th><td align="left"><input type='password' name='haslo'></td></tr>
  20. <tr><td align="center" colspan="2"><input type='submit' value='Wyślij' name='logowanie'></td></tr>
  21. </table>
  22. </form>
  23. <?php
  24. }
  25. ?>
  26. </body>
  27. </html>


sprawdz.php

  1. <?php session_start(); include("config.php"); ?>
  2. <html>
  3. <head>
  4. <title>Proste Logowanie</title>
  5. <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  6. </head>
  7. <body>
  8. <?php
  9. if($_POST['logowanie']){
  10.  $wys_login = htmlspecialchars($_POST['login']);
  11.  $wys_pass = htmlspecialchars($_POST['haslo']);
  12.  if($wys_login == $login && $wys_pass == $pass){
  13.    $_SESSION['user'] = $login;
  14.    header("Location:index.php");
  15.  }
  16.  else{
  17.    header("Location:index.php");
  18.  }
  19. }
  20. else{
  21.  header("Location:index.php");
  22. }
  23. ?>
  24. </body>
  25. </html>


logout.php
  1. <?php session_start(); ?>
  2. <html>
  3. <head>
  4. <title>Proste Logowanie</title>
  5. <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  6. </head>
  7. <body>
  8. <?php
  9. if(isSet($_SESSION['user']))
  10. {
  11.  echo "Wylogowano Poprawnie<br>";
  12.  echo "<a href=\"index.php\">Strona Główna</a>";
  13. }
  14. else{
  15.  header("Location:index.php");
  16. }
  17. ?>
  18. </body>
  19. </html>


  1. <?php
  2. $login = "login";
  3. $pass = "pass";
  4. ?>


Coś nie trybi, sciana.gif może ktoś coś pomoże biggrin.gif
sebekzosw
funkcja header(); powinna być przed kodem HTML smile.gif

sprawdz.php
  1. <?php
  2. include("config.php");
  3.  
  4. if($_POST['logowanie']){
  5.    $wys_login = htmlspecialchars($_POST['login']);
  6.     $wys_pass = htmlspecialchars($_POST['haslo']);
  7.     if($wys_login == $login && $wys_pass == $pass){
  8.           $_SESSION['user'] = $login;
  9.           header("Location:index.php");
  10.     } else {
  11.           header("Location:index.php");
  12.     }
  13. } else{
  14.     header("Location:index.php");
  15. }
  16. ?>


logout.php

  1. <?php
  2. $komunikat = '';
  3. if(isSet($_SESSION['user'])){
  4.     $komunikat = "Wylogowano Poprawnie<br><a href=\"index.php\">Strona Główna</a>";
  5.     session_destroy();
  6. } else{
  7.     header("Location:index.php");
  8. }
  9. ?>
  10.  
  11. <html>
  12.    <head>
  13.        <title>Proste Logowanie</title>
  14.        <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  15.    </head>
  16.    
  17.    <body>
  18.        <?=$komunikat?>
  19.    </body>
  20. </html>
MateuszS
Nie musi, ale musisz dać na poczatku strony ob_start(); a na samym koncu ob_end_flush();
omeck
Cytat(MateuszScirka @ 9.07.2009, 18:36:55 ) *
Nie musi, ale musisz dać na poczatku strony ob_start(); a na samym koncu ob_end_flush();

Dlaczego musi dać ob_start? snitch.gif
erix
Bo tak jest po najmniejszej linii oporu. tongue.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.