[PHP] Cookies werken niet

Pagina: 1
Acties:
  • 43 views sinds 30-01-2008

  • Isengrim
  • Registratie: Februari 2004
  • Laatst online: 15:43
Ik ben bezig met een inlog script waarin ik ook cookies gebruik, alleen gaat het niet goed want als ik de cookie in een 2e pagina opvraag dan komt er nix.
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?
ob_start();

if(!$_POST['inlog'])
{

  echo"<form  action='".$_SERVER['PHP_SELF']."?do=login' method='POST'>";
  ?>
   <br>
   login:
   <br>
   <input name="username" type="text" >
   <br>
   wachtwoord:
   <br>
   <input name="password" type="password" >
   <br>
   <input type='submit' value='login' method='POST' name='inlog'>
   <input type='checkbox' name='stay'>
   </form>
 <?
}
if($_GET['do'] == 'login')
{
   if($_SERVER['REQUEST_METHOD'] == 'POST');
   {

        if($_POST['username']== '')
        {
        echo'Vul een username in';
        exit;
        }

        if($_POST['password']== '')
        {
        echo'Vul een een wachtwoord in';
        exit;
        }


   mysql_connect('localhost', '', '');
   mysql_select_db('project');

   $username = $_POST['username'];
   $password = $_POST['password'];

   $inlog = "SELECT*FROM members WHERE username = '".$username."' AND password = '".$password."'";
   $check = mysql_query($inlog) or die(mysql_error());
   $count = mysql_num_rows($check);

   $gegevens = mysql_fetch_object($check);
   if($count == 1)
   {
   echo"Je bent nu ingelogd als <b>".$username."</B><br>";



    echo $gegevens->id;
    echo $gegevens->userlvl;
    echo"<br><br>";
    echo time();

    $hash = md5(rand(0,99999). $gegevens->id. $_SERVER['REMOTE_ADDR']);

    echo"<br><br>".$hash;


    $userid = $gegevens->id;
    $naam = $gegevens->naam;
    $ip = $_SERVER['REMOTE_ADDR'];



    $inlog = "SELECT*FROM members WHERE username = '".$username."' AND password = '".$password."'";
    $check = mysql_query($inlog) or die(mysql_error());
    $count = mysql_num_rows($check);


     $sql = 'SELECT * FROM `loggedin` WHERE userid ='.$userid.'';
     $kijkna = mysql_query($sql);
     $aantal = mysql_num_rows($kijkna) ;

     if($aantal != 1)
     {
        $input = "INSERT INTO loggedin (id, userid, Naam, tijd, hash, ip) VALUES ('', '$userid','$naam',NOW(),'$hash','$ip')";
        $voerin = mysql_query($input) or die("Er is is fout gegaan tijdens registreren van de sessie");
     }

     else
     {

        $login = "UPDATE loggedin SET hash = '$hash', ip = '$ip', tijd= NOW() WHERE userid = ".$userid."" ; //(id, userid, Naam, tijd, hash, ip) VALUES ('', '$userid','$naam','time()','$hash','$ip')";
        $update = mysql_query($login) or die (mysql_error ());
    }

        setcookie('test_hash',$hash,time()+3600,'/test','localhost');
        setcookie('test_user_id',$userid,time()+3600,'/test','loclahost');

        ob_end_flush();
        echo"<br><br><br><hr>";
        echo $test_hash;
        echo $test_user_id;


   }
   else
   {
   echo" Je hebt een verkeerde username of password ingevuld";
   }
}

}

?>


De pagina waarin ik de cookie opvraag:
PHP:
1
2
3
4
5
6
7
<?php

echo $_COOKIE['test_hash'];
echo "<br>";
echo $_COOKIE['test_user_id'];

?>

  • NMe
  • Registratie: Februari 2004
  • Laatst online: 24-05 14:53

NMe

Quia Ego Sic Dico.

Wij zijn er niet om je code te debuggen hoor. :/
Probeer eens een echo onder het setten van de cookies om te kijken of ze überhaupt wel geset worden.

'E's fighting in there!' he stuttered, grabbing the captain's arm.
'All by himself?' said the captain.
'No, with everyone!' shouted Nobby, hopping from one foot to the other.


  • Brons
  • Registratie: April 2002
  • Laatst online: 10:59

Brons

Fail!

Ik heb je code niet gelezen (beetje te veel van het goede tbh...), maar kijk eens of je cookie geset is door bv te echoen als NMe84 zegt. Of kijk ff of je cookie op je hdd staat, of kijk in je browser (Weet niet of IE aan cookies 'kijken' doet tho).

  • curry684
  • Registratie: Juni 2000
  • Laatst online: 12-05 22:23

curry684

left part of the evil twins

Debuggen doe je zelf maar. Zodra je concreet 5 regels code hebt waarin het fout gaat en er bij kunt vertellen waarom je de specifieke fout daarin zelf niet snapt willen we je wellicht wel helpen.

Professionele website nodig?


Dit topic is gesloten.