[PHP/MySQL]problem z cookie w skrypcie logowania
: wt lis 02, 2004 3:59 pm
Mam problem z logowaniem,(wersja skryptu ponizej jest z uzyciem cookie [aby po zalogowaniu sie nie trzeba bylo sie ponownie logowac gdziekolwiek na stronie przez 24h], ale przed wprowadzeniem cookie skrypt dzialal i mnie logowal). Jednak teraz gdy wpisze dane i wysle formularz, pojawia sie komunikat o blednym loginie i hasle (mimo ze podaje poprawne dane, ktora sa w bazie.
ponizej zawartosc register.php:
[php]
<?
session_start();
include("common.php");
global $id_polaczenia;
$id_polaczenia = db_connect();
if (!isset ($_POST["user_nick"]) && !isset ($_POST["user_password"]) && !$_SESSION["usernick"] && !$_SESSION["userpassword"]){
html_header();
?>
</TR>...
//formularz logowania
... </form>
<?php
html_footer();
}
else
setcookie ("user_id", "$_POST[user_nick]", time()+86400);
setcookie ("user_pass", "$_POST[user_password]", time()+86400);
$_SESSION["usernick"] = strtolower(trim($_COOKIE['user_id']));
$_SESSION["userpassword"] = trim($_COOKIE['user_pass']);
$zapytanie = mysql_query("SELECT user_nick, user_pass, admin FROM users WHERE user_nick ='$_SESSION[usernick]'");
$user = mysql_fetch_array($zapytanie);
if ($_SESSION["usernick"] == strtolower($user["user_nick"]) && $_SESSION["userpassword"] == $user["user_pass"] && $user["admin"] == "TAK"){
global $id_polaczenia;
$id_polaczenia = db_connect();
function in_use($user_nick) {
global $user_tablename;
$query = "SELECT user_nick FROM $user_tablename WHERE user_nick = '$user_nick'";
$wynik = mysql_query($query);
if (!mysql_num_rows($wynik)) return 0;
else return 1;
}
function reg_form() {
html_header();
?>
<TR>.....
// formularz rejestracji
...</form>
<?php
echo " <a href=\"$PHP_SELF?action=logout\"><font style=\"font-family: Verdana; font-size: 9px;\"><b>Wyloguj</b></a><br>\n";
html_footer();
}
function register_user() {
global $id_polaczenia;
global $nick, $userpassword, $userpassword2, $usermail, $userprofile, $userhardware, $admin;
global $default_db, $user_tablename;
$awatar = "obrazki/users/none.jpg";
mailcheck ($usermail);
if (empty ($userprofile) ) error_message ("Brak komentarza!");
if (empty ($usermail) ) error_message ("Brak adresu email!");
if (empty ($nick) ) error_message ("Brak nicku!");
if (empty ($userpassword) ) error_message ("Brak hasla!");
if (empty ($userpassword2) ) error_message ("Musisz powtórzyć hasło!");
if ($userpassword! = $userpassword2) {
error_message ("Podane hasła nie są identyczne");
}
if (in_use ($nick) ) {
error_message ("Wybrany nick jest już zajęty, proszę wybrać inny");
}
$query = "INSERT INTO users VALUES(NULL, '$nick', '$userpassword', '$usermail', '$userprofile', curdate(), NULL, '$userhardware', '$awatar', '$admin')";
$wynik = mysql_query ($query);
if (!$wynik) error_message (sql_error());
html_header();
?>
<b>Użytkownik <?php echo $nick ?> został zarejestrowany</b>
<?php
html_footer();
}
switch ($action) {
case "register_user":
register_user();
break;
case "logout":
logout();
break;
default:
reg_form();
break;
}
}
else {
session_unregister("usernick");
session_unregister("userpassword");
setcookie ("user_id");
setcookie ("user_pass");
echo "zly login lub haslo";
echo "<a href=\"$PHP_SELF\">Logowanie</a><br>";
}
?>
[/php]
funcja logout(), znajdujaca sie pliku common.php:
[php]
<?
function logout() {
if($_COOKIE['user_id'] && $_COOKIE['user_pass']) {
setcookie ("user_id");
setcookie ("user_pass");
session_unregister("userpassword");
session_unregister("usernick");
error_message("Zostałeś wylogowany");
header ("Location: index.php");
}
}
?>[/php]
thx
PS. Jesli by ktos wiedzial co jest nie tak z tym skryptem to prosze o pw/odpowiedz na tamtym forum/gg (1661185)/lub ewentualnie w tym temacie http://forum.php.pl/index.php?showtopic=22285
pzdr
ponizej zawartosc register.php:
[php]
<?
session_start();
include("common.php");
global $id_polaczenia;
$id_polaczenia = db_connect();
if (!isset ($_POST["user_nick"]) && !isset ($_POST["user_password"]) && !$_SESSION["usernick"] && !$_SESSION["userpassword"]){
html_header();
?>
</TR>...
//formularz logowania
... </form>
<?php
html_footer();
}
else
setcookie ("user_id", "$_POST[user_nick]", time()+86400);
setcookie ("user_pass", "$_POST[user_password]", time()+86400);
$_SESSION["usernick"] = strtolower(trim($_COOKIE['user_id']));
$_SESSION["userpassword"] = trim($_COOKIE['user_pass']);
$zapytanie = mysql_query("SELECT user_nick, user_pass, admin FROM users WHERE user_nick ='$_SESSION[usernick]'");
$user = mysql_fetch_array($zapytanie);
if ($_SESSION["usernick"] == strtolower($user["user_nick"]) && $_SESSION["userpassword"] == $user["user_pass"] && $user["admin"] == "TAK"){
global $id_polaczenia;
$id_polaczenia = db_connect();
function in_use($user_nick) {
global $user_tablename;
$query = "SELECT user_nick FROM $user_tablename WHERE user_nick = '$user_nick'";
$wynik = mysql_query($query);
if (!mysql_num_rows($wynik)) return 0;
else return 1;
}
function reg_form() {
html_header();
?>
<TR>.....
// formularz rejestracji
...</form>
<?php
echo " <a href=\"$PHP_SELF?action=logout\"><font style=\"font-family: Verdana; font-size: 9px;\"><b>Wyloguj</b></a><br>\n";
html_footer();
}
function register_user() {
global $id_polaczenia;
global $nick, $userpassword, $userpassword2, $usermail, $userprofile, $userhardware, $admin;
global $default_db, $user_tablename;
$awatar = "obrazki/users/none.jpg";
mailcheck ($usermail);
if (empty ($userprofile) ) error_message ("Brak komentarza!");
if (empty ($usermail) ) error_message ("Brak adresu email!");
if (empty ($nick) ) error_message ("Brak nicku!");
if (empty ($userpassword) ) error_message ("Brak hasla!");
if (empty ($userpassword2) ) error_message ("Musisz powtórzyć hasło!");
if ($userpassword! = $userpassword2) {
error_message ("Podane hasła nie są identyczne");
}
if (in_use ($nick) ) {
error_message ("Wybrany nick jest już zajęty, proszę wybrać inny");
}
$query = "INSERT INTO users VALUES(NULL, '$nick', '$userpassword', '$usermail', '$userprofile', curdate(), NULL, '$userhardware', '$awatar', '$admin')";
$wynik = mysql_query ($query);
if (!$wynik) error_message (sql_error());
html_header();
?>
<b>Użytkownik <?php echo $nick ?> został zarejestrowany</b>
<?php
html_footer();
}
switch ($action) {
case "register_user":
register_user();
break;
case "logout":
logout();
break;
default:
reg_form();
break;
}
}
else {
session_unregister("usernick");
session_unregister("userpassword");
setcookie ("user_id");
setcookie ("user_pass");
echo "zly login lub haslo";
echo "<a href=\"$PHP_SELF\">Logowanie</a><br>";
}
?>
[/php]
funcja logout(), znajdujaca sie pliku common.php:
[php]
<?
function logout() {
if($_COOKIE['user_id'] && $_COOKIE['user_pass']) {
setcookie ("user_id");
setcookie ("user_pass");
session_unregister("userpassword");
session_unregister("usernick");
error_message("Zostałeś wylogowany");
header ("Location: index.php");
}
}
?>[/php]
thx
PS. Jesli by ktos wiedzial co jest nie tak z tym skryptem to prosze o pw/odpowiedz na tamtym forum/gg (1661185)/lub ewentualnie w tym temacie http://forum.php.pl/index.php?showtopic=22285
pzdr