Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [MySQL][PHP]Problem z logowaniem na konkretną stronkę.
Forum PHP.pl > Forum > Przedszkole
woxala123
Mam taki plik logowania login.php. Jak przerobić, by np: admin wchodził na swoją stronę admin.php a user na stronę profile.php.
Wybaczcie ale jestem jeszcze przedszkolakiem w dziedzinie php. Tak myslałem o przekierowaniu z poziomu level ale jak to zrobić?
Proszę o wszelaką pomoc i zgóry dziękuje za zainteresowanie się tematem.
login.php

  1. <?php require_once('Connections/local.php'); ?>
  2. <?php
  3. if (!function_exists("GetSQLValueString")) {
  4. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  5. {
  6. if (PHP_VERSION < 6) {
  7. $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  8. }
  9.  
  10. $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  11.  
  12. switch ($theType) {
  13. case "text":
  14. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  15. break;
  16. case "long":
  17. case "int":
  18. $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  19. break;
  20. case "double":
  21. $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  22. break;
  23. case "date":
  24. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  25. break;
  26. case "defined":
  27. $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  28. break;
  29. }
  30. return $theValue;
  31. }
  32. }
  33. ?>
  34. <?php
  35. // *** Validate request to login to this site.
  36. if (!isset($_SESSION)) {
  37. }
  38.  
  39. $loginFormAction = $_SERVER['PHP_SELF'];
  40. if (isset($_GET['accesscheck'])) {
  41. $_SESSION['PrevUrl'] = $_GET['accesscheck'];
  42. }
  43.  
  44. if (isset($_POST['user'])) {
  45. $loginUsername=$_POST['user'];
  46. $password=$_POST['pass'];
  47. $MM_fldUserAuthorization = "level";
  48. $MM_redirectLoginSuccess = "profile.php";
  49. $MM_redirectLoginFailed = "register.php";
  50. $MM_redirecttoReferrer = false;
  51. mysql_select_db($database_local, $local);
  52.  
  53. $LoginRS__query=sprintf("SELECT username, password, level FROM login WHERE username=%s AND password=%s",
  54. GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));
  55.  
  56. $LoginRS = mysql_query($LoginRS__query, $local) or die(mysql_error());
  57. $loginFoundUser = mysql_num_rows($LoginRS);
  58. if ($loginFoundUser) {
  59.  
  60. $loginStrGroup = mysql_result($LoginRS,0,'level');
  61.  
  62. if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
  63. //declare two session variables and assign them
  64. $_SESSION['MM_Username'] = $loginUsername;
  65. $_SESSION['MM_UserGroup'] = $loginStrGroup;
  66.  
  67. if (isset($_SESSION['PrevUrl']) && false) {
  68. $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
  69. }
  70. header("Location: " . $MM_redirectLoginSuccess );
  71. }
  72. else {
  73. header("Location: ". $MM_redirectLoginFailed );
  74. }
  75. }
  76. ?>
  77. <form name="form1" method="POST" action="<?php echo $loginFormAction; ?>">
  78. <div align="center">
  79. <p>&nbsp;</p>
  80. <p>&nbsp;</p>
  81. <p>&nbsp;</p>
  82. <p>&nbsp;</p>
  83. <p>&nbsp;</p>
  84. <p>&nbsp;</p>
  85. <table width="415" border="0" cellspacing="0" cellpadding="0">
  86. <tr>
  87. <td width="91"><label for="user">
  88. <div align="center">Username:
  89. </label>
  90. </div></td>
  91. <td width="324"><div align="left">
  92. <input type="text" name="user" id="user">
  93. </div></td>
  94. </tr>
  95. <tr>
  96. <td height="29"><label for="pass">
  97. <div align="center">Password:
  98. </label>
  99. </div></td>
  100. <td><div align="left">
  101. <input type="password" name="pass" id="pass">
  102. </div></td>
  103. </tr>
  104. <tr>
  105. <td colspan="2"><div align="center">
  106. <input type="submit" name="submit" id="submit" value="Login">
  107. </div></td>
  108. </tr>
  109. </table>
  110. <p>&nbsp;</p>
  111. <p>&nbsp;</p>
  112. <p>&nbsp;</p>
  113. <p>&nbsp;</p>
  114. </div>
  115. </form>
  116.  
  117.  
Turson
W tabeli users dodaj kolumnę admin, domyślnie wartość 0. Nadaj wartość 1 konkretnemu użytownikowi, który będzie adminem.
Przy logowaniu sprawdź czy admin w tym koncie równe jest 1. Jeśli tak - zapisz w sesji np. $_SESSION["admin"]==true
Potem na danej podstronie z panelen admina sprawdź wartość z $_SESSION["admin"].
woxala123
Okey dodam kolumnę admin i dam adminowi wartosś 1 w kolumnie admin 1. Ale gdzie zrobić przekierowanie w pliku login.php i jak to zrobić?
Turson
Sprawdzasz warunkiem czy jest adminem. Przekierowanie
  1. header('Location: link');
woxala123
gdzie to wpisać i w którym miejscu i co dokładnie?
Turson
Coś w tym stylu
  1. <?PHP
  2. if(!isset($_SESSION["admin"])){ $_SESSION["admin"]==false;}
  3. ?>
  4. <html>
  5. <body>
  6. <?PHP
  7. if($_SESSION["admin"]==true))
  8. {
  9. echo "Jesteś adminem";
  10. }
  11. else
  12. echo "Nie jesteś adminem";
  13. ?>
  14. </body>
  15. </html>
woxala123
Okey fajnie w której linijce i gdzie dopisac przekierowanie
Turson
To już zależy co chcesz osiągnąc: ma przekierować admina czy zwykłego usera. Zamiast echo...
woxala123
Okey fajnie. Tylko podpowiedz jeśli Ciebie można prosić w której linijce i co wywalić to wtedy myslę tak że zamkniemy temat.
Turson
Cytat(TursoN @ 25.08.2013, 14:06:12 ) *
To już zależy co chcesz osiągnąc: ma przekierować admina czy zwykłego usera. Zamiast echo...

woxala123
Jeszcze raz Ciebie proszę i pytam. Czy jestes w stanie pomóc do końca. ? Zrobiłem kolumne admin nadałem domyślnie wartość 0.
Teraz tak czy całą procedurę którą napisałeś zacząć od linijki 68. I tak natym zakończyc ? Czy coś jeszcze poprawić?
  1. <?PHP
  2. if(!isset($_SESSION["admin"])){ $_SESSION["admin"]==false;}
  3. ?>
  4. <html>
  5. <body>
  6. <?PHP
  7. if($_SESSION["admin"]==true))
  8. {
  9. header('Location:admin.php');
  10. }
  11. else
  12. header('Location:profile');
  13. ?>
  14. </body>
  15. </html>
Turson
W login.php:
-pobierz wartość `admin` dla usera który się zalogował
-sprawdź czy admin=1
jeśli tak: $_SESSION["admin"]==true;
jeśli nie: $_SESSION["admin"]==false;

Kod, co podałeś jest ok.

Pamiętaj o:
-przy ładowaniu profilu admina sprawdzaj czy on nim faktycznie jest
-przy wylogowaniu ustaw sesję admina na false
woxala123
Teraz już tak szczerze powiem "Nic nie wiem". Proszę kto dokończy ten kod?. Siedzę już na nad tym 2 dzień.
Turson
Czego nie wiesz? Wszystko podałem. My tu gotowców nie piszemy.
woxala123
Super już dokończyłęm do końca. Tylko czy tak moze być.
  1. <?php require_once('Connections/local.php'); ?>
  2. <?php
  3. if (!function_exists("GetSQLValueString")) {
  4. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  5. {
  6. if (PHP_VERSION < 6) {
  7. $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  8. }
  9.  
  10. $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  11.  
  12. switch ($theType) {
  13. case "text":
  14. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  15. break;
  16. case "long":
  17. case "int":
  18. $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  19. break;
  20. case "double":
  21. $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  22. break;
  23. case "date":
  24. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  25. break;
  26. case "defined":
  27. $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  28. break;
  29. }
  30. return $theValue;
  31. }
  32. }
  33. ?>
  34. <?php
  35. // *** Validate request to login to this site.
  36. if (!isset($_SESSION)) {
  37. }
  38.  
  39. $loginFormAction = $_SERVER['PHP_SELF'];
  40. if (isset($_GET['accesscheck'])) {
  41. $_SESSION['PrevUrl'] = $_GET['accesscheck'];
  42. }
  43.  
  44. if (isset($_POST['user'])) {
  45. $loginUsername=$_POST['user'];
  46. $password=$_POST['pass'];
  47. $MM_fldUserAuthorization = "admin";
  48. $MM_redirectLoginSuccess = "profile.php";
  49. $MM_redirectLoginFailed = "register.php";
  50. $MM_redirecttoReferrer = false;
  51. mysql_select_db($database_local, $local);
  52.  
  53. $LoginRS__query=sprintf("SELECT username, password, admin FROM login WHERE username=%s AND password=%s",
  54. GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));
  55.  
  56. $LoginRS = mysql_query($LoginRS__query, $local) or die(mysql_error());
  57. $loginFoundUser = mysql_num_rows($LoginRS);
  58. if ($loginFoundUser) {
  59.  
  60. $loginStrGroup = mysql_result($LoginRS,0,'admin');
  61.  
  62. if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
  63. ?>
  64. //declare two session variables and assign them
  65. <html>
  66. <body>
  67. <?PHP
  68. if(!isset($_SESSION["admin"])){ $_SESSION["admin"]==1;}
  69. if($_SESSION["admin"]==0)
  70. {
  71. header('Location:../bravo/_admin/admin.php');
  72. }
  73. else
  74. header('Location:index.php');
  75. }
  76. }
  77. ?>
  78.  
  79. <form name="form1" method="POST" action="<?php echo $loginFormAction; ?>">
  80. <div align="center">
  81. <p>&nbsp;</p>
  82. <p>&nbsp;</p>
  83. <p>&nbsp;</p>
  84. <p>&nbsp;</p>
  85. <p>&nbsp;</p>
  86. <p>&nbsp;</p>
  87. <table width="415" border="0" cellspacing="0" cellpadding="0">
  88. <tr>
  89. <td width="91"><label for="user">
  90. <div align="center">Username:
  91. </label>
  92. </div></td>
  93. <td width="324"><div align="left">
  94. <input type="text" name="user" id="user">
  95. </div></td>
  96. </tr>
  97. <tr>
  98. <td height="29"><label for="pass">
  99. <div align="center">Password:
  100. </label>
  101. </div></td>
  102. <td><div align="left">
  103. <input type="password" name="pass" id="pass">
  104. </div></td>
  105. </tr>
  106. <tr>
  107. <td colspan="2"><div align="center">
  108. <input type="submit" name="submit" id="submit" value="Login">
  109. </div></td>
  110. </tr>
  111. </table>
  112. <p>&nbsp;</p>
  113. <p>&nbsp;</p>
  114. <p>&nbsp;</p>
  115. <p>&nbsp;</p>
  116. </div>
  117. </form>
  118.  
Turson
Wynika z tego:
1 - zwykły user
0 - admin

Najważniejsze to sprawdź czy działa.
woxala123
Nie wiem co się dzieje. Bo gdy birę ograniczenie dostępu z poziomu admin to nie chce się logować na wybrana stronę a jak wezme z poziomu usera i hasła to wchodzi. O co tu chodzi pytam się?

Nie wiem co się dzieje. Bo gdy birę ograniczenie dostępu z poziomu admin to nie chce się logować na wybrana stronę a jak wezme z poziomu usera i hasła to wchodzi. O co tu chodzi pytam się?
Turson
Bo masz
1 - user
0 - admin
woxala123
To nawet to zmienię to samo.

temat rozwiązany oto kod ostateczny. Nikt tego w internecie nie umie rozwiązać. A rozwiązanie wygląda na banalnie proste. Wystarczy w ograniczeniu dostępu w miarę niepowodzenia logowania przekierować usera który nie ma prawa admina na jego profile.php. i Zmienić 2 linijki kodu w login.php. Miłego dnia.
  1. <?php
  2. if (!function_exists("GetSQLValueString")) {
  3. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  4. {
  5. if (PHP_VERSION < 6) {
  6. $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  7. }
  8.  
  9. $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  10.  
  11. switch ($theType) {
  12. case "text":
  13. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  14. break;
  15. case "long":
  16. case "int":
  17. $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  18. break;
  19. case "double":
  20. $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  21. break;
  22. case "date":
  23. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  24. break;
  25. case "defined":
  26. $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  27. break;
  28. }
  29. return $theValue;
  30. }
  31. }
  32. ?>
  33. <?php
  34. // *** Validate request to login to this site.
  35. if (!isset($_SESSION)) {
  36. }
  37.  
  38. $loginFormAction = $_SERVER['PHP_SELF'];
  39. if (isset($_GET['accesscheck'])) {
  40. $_SESSION['PrevUrl'] = $_GET['accesscheck'];
  41. }
  42.  
  43. if (isset($_POST['user'])) {
  44. $loginUsername=$_POST['user'];
  45. $password=$_POST['pass'];
  46. $MM_fldUserAuthorization = "admin";
  47. $MM_redirectLoginSuccess = "profile.php";
  48. $MM_redirectLoginFailed = "profile.php";
  49. $MM_redirecttoReferrer = false;
  50. mysql_select_db($database_local, $local);
  51.  
  52. $LoginRS__query=sprintf("SELECT username, password, admin FROM login WHERE username=%s AND password=%s",
  53. GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));
  54.  
  55. $LoginRS = mysql_query($LoginRS__query, $local) or die(mysql_error());
  56. $loginFoundUser = mysql_num_rows($LoginRS);
  57. if ($loginFoundUser) {
  58.  
  59. $loginStrGroup = mysql_result($LoginRS,0,'admin');
  60.  
  61. if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
  62. //declare two session variables and assign them
  63. $_SESSION['MM_Username'] = $loginUsername;
  64. $_SESSION['MM_UserGroup'] = $loginStrGroup;
  65.  
  66. if (isset($_SESSION['PrevUrl']) && false) {
  67. $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
  68. }
  69. header("Location: _admin/admin.php" );
  70. }
  71. else {
  72. header("Location: profile.php");
  73. }
  74. }
  75. ?>
  76.  
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.