Results 1 to 9 of 9
Thread: PHP -
Hybrid View
-
13th March 2013 16:35 #1
PHP -
login.php
index.phpPHP Code:if ($row = mysql_fetch_array($q))
{
if($rememberme == "on"){
setcookie("username",$row['username'],time()+7200);
}else if($rememberme == "undefined"){
$_SESSION['username'] = $row["username"];
}
header ("Location: ".$_POST['curPage']);
}
, setcookie , , Block 4PHP Code:if(isset($_SESSION['username'])) { // Block 1
echo '<div id="lognat"><ul><li></li>,<li><a href="login/members.php">'. $_SESSION['username'].'</a></li></ul>';
echo '<p><a href="login/logout.php"></a></p></div>';
}
else if(isset($_COOKIE['username'])) { // Block 2
echo '<div id="lognat"><ul><li></li>,<li><a href="login/members.php">'. $_COOKIE['username'].'</a></li></ul>';
echo '<p><a href="login/logout.php"></a></p></div>';
}
else if(isset($_SESSION['error'])){ // Block 3
echo $_SESSION['error'];
session_destroy() ;
require_once('form.php');
}
else{ // Block 4
require_once('form.php');
}
, , Block 1 .
- , Block 2 Block 4.
, .
-
13th March 2013 17:16 #2Registered User
Join Date: Dec:2003
Posts: 2,817
. $_SESSION['error']? 99% 1 2? $rememberme ( , register_globals e on)?
Gigabyte DS3P, E4400@9x333, 2x2Gb Super T@1000Mhz, Gigabyte GTX460OC 768Mb
-
13th March 2013 17:25 #3
$_SESSION['error'] ,
1 , 2 .
$rememberme -
register_globals
- ,
. , 2 4
-
13th March 2013 17:41 #4Registered User
Join Date: Jul:2005
Location: Sofiq
Posts: 2,798
setcookie . setcookie . .
-
13th March 2013 23:11 #5
login.php
PHP Code:<?php
session_start();
require ("dbConfig.php");
$username = addslashes($_POST['username']);
$password = $_POST['password'];
if(isset($_POST['rememberme'])){
$rememberme = $_POST['rememberme'];
}else{
$rememberme = "undefined";
}
if(!empty ($password))
{
$password = md5(addslashes($password));
}
if($_SERVER['REQUEST_METHOD'] != 'POST') header('Location:../index.php');
$q = mysql_query("SELECT username, password FROM users where username = '$username' AND password = '$password' Limit 1"); //
if ($row = mysql_fetch_array($q)) {
if($rememberme == "on"){
setcookie("username",$row['username'],time()+7200);
}else if($rememberme == "undefined"){
$_SESSION['username'] = $row["username"];
}
header ("Location: ".$_POST['curPage']);
}else{
if (empty($username)){
$_SESSION['error'] ='<div class="mistake"> </div>';
header ("Location:".$_POST['curPage']);
}
else if (empty($password)){
$_SESSION['error'] = '<div class="mistake"> </div>';
header ("Location:".$_POST['curPage']);
}
else{
$_SESSION['error'] = '<div class="mistake"> </div>';
header("Location: ".$_POST['curPage']);
}
}
?>
-
-
14th March 2013 09:03 #6Registered User
Join Date: Jul:2005
Location: Sofiq
Posts: 2,798
. , localhost 127.0.0.1. domain.
php.net setcookie.
something that wasn't made clear to me here and totally confused me for a while was that domain names must contain at least two dots (.), hence 'localhost' is invalid and the browser will refuse to set the cookie! instead for localhost you should use false.
to make your code work on both localhost and a proper domain, you can do this:
<?php
$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;
setcookie('cookiename', 'data', time()+60*60*24*365, '/', $domain, false);
?>




Reply With Quote
Lenovo ThinkPad 15 IdeaPad 15
5th May 2023, 22:16 in