Waarom werk setcookie() niet??
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Log a user on
function login($connection, $uid, $pwd, $remember=FALSE, $closeConnection=FALSE) {
// Take some security measures
$pwd = md5($pwd);
// Get the userdata
$user = finduser($connection, $uid);
if ($user == NULL) {
if ($closeConnection) {
mysql_close($connection);
}
return FALSE;
}
// Match the password
if ($pwd == $user['pwd']) { // The password matches
if ($remember) {
setcookie('uid', $uid, time()+(60*60*24*30)); // Remember the uid for 30 days
}
if ($closeConnection) {
mysql_close($connection);
}
return TRUE;
} else { // The password does not match
$_SESSION['notification'] = "The password is incorrect.";
if ($closeConnection) {
mysql_close($connection);
}
return FALSE;
}
}
function login($connection, $uid, $pwd, $remember=FALSE, $closeConnection=FALSE) {
// Take some security measures
$pwd = md5($pwd);
// Get the userdata
$user = finduser($connection, $uid);
if ($user == NULL) {
if ($closeConnection) {
mysql_close($connection);
}
return FALSE;
}
// Match the password
if ($pwd == $user['pwd']) { // The password matches
if ($remember) {
setcookie('uid', $uid, time()+(60*60*24*30)); // Remember the uid for 30 days
}
if ($closeConnection) {
mysql_close($connection);
}
return TRUE;
} else { // The password does not match
$_SESSION['notification'] = "The password is incorrect.";
if ($closeConnection) {
mysql_close($connection);
}
return FALSE;
}
}
Ik hoop dat iemand begrijpt waarom het niet werkt en mij kan vertellen hoe ik het wel kan laten werken. Bij voorbaat dank.
Mvg,
Mark
Graag in het vervolg bij code, [code] [/code] tags gebruiken. [/modedit]
Gewijzigd op 02/12/2010 19:00:06 door Bas IJzelendoorn
ob_start()?
ob_start == evil
@Mark, begin eens met onderstaande code boven aan je scipt. En gebruik $_COOKIE i.p.v. setcookie
Na het zetten van de cookies, kan je best, bv. met header('location ...') de boel verversen.
Het gebruiken van $_COOKIE ipv setcookie() gaat niet lukken omdat $_COOKIE variabelen alleen tijdens de sessie blijven bestaan (blijkbaar), heb het net geprobeerd. Met setcookie() zou de cookie moeten blijven staan voor 30 dagen... Toch?
Kan het trouwens komen doordat het php bestand die setcookie() aanroept in een subdirectory staat van de map met het php bestand die de cookie probeert op te halen?
Toon eens hoe je de functie aanroept (en wat code er voor en er na).
Code (php)
1
setcookie('uid', $uid, time()+(60*60*24*30), '/', $_SERVER['HTTP_HOST']); // Remember the uid for 30 days
Werkt ook niet.
Toevoeging op 01/12/2010 14:46:40:
@Kris, de onderstaande code is een deel van login.php, de login functie zoals hierboven beschreven staat in users.php. Deze 2 php bestanden staan in de /system directory. De scripts die de cookie proberen op te halen staan in de root.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$user['uid'] = $_POST['uid'];
$user['pwd'] = $_POST['pwd'];
// Connect to the MySql server
require_once '../settings/mysql.php';
$connection = mysql_connect($settings['mysql']['address'], $settings['mysql']['uid'], $settings['mysql']['pwd']);
// Log the user on
include 'users.php';
if (login($connection, $user['uid'], $user['pwd'], $_POST['remember_uid'], TRUE)) {
header("location: ../dashboard.php?uid=". urlencode($user['uid']));
exit;
} else {
header("location: ../index.php?notification=". $_SESSION['notification']);
exit;
}
$user['pwd'] = $_POST['pwd'];
// Connect to the MySql server
require_once '../settings/mysql.php';
$connection = mysql_connect($settings['mysql']['address'], $settings['mysql']['uid'], $settings['mysql']['pwd']);
// Log the user on
include 'users.php';
if (login($connection, $user['uid'], $user['pwd'], $_POST['remember_uid'], TRUE)) {
header("location: ../dashboard.php?uid=". urlencode($user['uid']));
exit;
} else {
header("location: ../index.php?notification=". $_SESSION['notification']);
exit;
}
Graag in het vervolg bij code, [code] [/code] tags gebruiken. [/modedit]
Gewijzigd op 02/12/2010 19:00:43 door Bas IJzelendoorn
Dit werkt stand alone
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
// Log a user on
function login($connection, $uid, $pwd, $remember=FALSE, $closeConnection=FALSE) {
// Take some security measures
$pwd = md5($pwd);
// Get the userdata
$user = finduser($connection, $uid);
if ($user == NULL) {
if ($closeConnection) {
mysql_close($connection);
}
return FALSE;
}
// Match the password
if ($pwd == $user['pwd']) { // The password matches
if ($remember) {
setcookie('uid', $uid, time()+(60*60*24*30)); // Remember the uid for 30 days
}
if ($closeConnection) {
mysql_close($connection);
}
return TRUE;
} else { // The password does not match
$_SESSION['notification'] = "The password is incorrect.";
if ($closeConnection) {
mysql_close($connection);
}
return FALSE;
}
}
function finduser($connection, $uid) {
// hier even zonder mysql
$users = array(
0 => array(
'pwd' => md5('pass0')
),
1 => array(
'pwd' => md5('pass1')
),
2 => array(
'pwd' => md5('pass2')
),
);
return $users[$uid];
}
if (!empty($_POST)) {
$user['uid'] = $_POST['uid'];
$user['pwd'] = $_POST['pwd'];
// Connect to the MySql server
// require_once '../settings/mysql.php';
// $connection = mysql_connect($settings['mysql']['address'], $settings['mysql']['uid'], $settings['mysql']['pwd']);
$connection = null;
// Log the user on
// include 'users.php';
if (login($connection, $user['uid'], $user['pwd'], $_POST['remember_uid'], false)) {
header("location: ?uid=". urlencode($user['uid']));
exit;
} else {
header("location: ?notification=". $_SESSION['notification']);
exit;
}
}
else {
echo '
<form method="post" action="">
<input name="uid" /> uid
<input name="pwd" /> pwd
<input type="checkbox" name="remember_uid" /> remember
<input type="submit" value="OK">
</form>
Je kan inloggen met
<table>
<tr><th>uid</th><th>pass</th></tr>
<tr><td>0</td><td>pass0</td></tr>
<tr><td>1</td><td>pass1</td></tr>
<tr><td>2</td><td>pass2</td></tr>
</table>
';
}
?>
// Log a user on
function login($connection, $uid, $pwd, $remember=FALSE, $closeConnection=FALSE) {
// Take some security measures
$pwd = md5($pwd);
// Get the userdata
$user = finduser($connection, $uid);
if ($user == NULL) {
if ($closeConnection) {
mysql_close($connection);
}
return FALSE;
}
// Match the password
if ($pwd == $user['pwd']) { // The password matches
if ($remember) {
setcookie('uid', $uid, time()+(60*60*24*30)); // Remember the uid for 30 days
}
if ($closeConnection) {
mysql_close($connection);
}
return TRUE;
} else { // The password does not match
$_SESSION['notification'] = "The password is incorrect.";
if ($closeConnection) {
mysql_close($connection);
}
return FALSE;
}
}
function finduser($connection, $uid) {
// hier even zonder mysql
$users = array(
0 => array(
'pwd' => md5('pass0')
),
1 => array(
'pwd' => md5('pass1')
),
2 => array(
'pwd' => md5('pass2')
),
);
return $users[$uid];
}
if (!empty($_POST)) {
$user['uid'] = $_POST['uid'];
$user['pwd'] = $_POST['pwd'];
// Connect to the MySql server
// require_once '../settings/mysql.php';
// $connection = mysql_connect($settings['mysql']['address'], $settings['mysql']['uid'], $settings['mysql']['pwd']);
$connection = null;
// Log the user on
// include 'users.php';
if (login($connection, $user['uid'], $user['pwd'], $_POST['remember_uid'], false)) {
header("location: ?uid=". urlencode($user['uid']));
exit;
} else {
header("location: ?notification=". $_SESSION['notification']);
exit;
}
}
else {
echo '
<form method="post" action="">
<input name="uid" /> uid
<input name="pwd" /> pwd
<input type="checkbox" name="remember_uid" /> remember
<input type="submit" value="OK">
</form>
Je kan inloggen met
<table>
<tr><th>uid</th><th>pass</th></tr>
<tr><td>0</td><td>pass0</td></tr>
<tr><td>1</td><td>pass1</td></tr>
<tr><td>2</td><td>pass2</td></tr>
</table>
';
}
?>
Bij mij wordt de cookie wel bijgehouden.
Bij mij werkt het nog steeds niet