[PHP] Probleempje

Gestart doorJosnl12 reacties27 weergaven

13 berichten · pagina 1 van 1

Josnl

Lid · Topicstarter

Berichten
3.461
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Heey allemaal!

Ik heb net een loginsysteempje gemaakt, en dat werkt allemaal.

Alleen wil ik dat als je correcte gegevens hebt ingevuld dat ie dan een sessie of cookie aanmaakt, alleen heb ik geen idee hoe ik dat moet doen.bloos
Dit is mijn code (werkt):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Inloggen</title>
</head>

<body>


[COLOR= #0000BB]<?php


$gebruiker[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$_POST[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]naam[/COLOR][COLOR= #007700]];[/COLOR]
[COLOR= #0000BB]$wachtwoord[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$_POST[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]pass[/COLOR][COLOR= #007700]];[/COLOR]

[URL="http://www.php.net/if"]if[/URL] [COLOR= #007700]([/COLOR][COLOR= #0000BB]$gebruiker[/COLOR] [COLOR= #007700]===[/COLOR] test [COLOR= #007700]&&[/COLOR] [COLOR= #0000BB]$wachtwoord[/COLOR] [COLOR= #007700]===[/COLOR] demo[COLOR= #007700]){[/COLOR]

        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]<font color="#009900">Je bent succesvol ingelogd!</font>[/COLOR][COLOR= #007700];
        
        }[/COLOR]
    
        [URL="http://www.php.net/elseif"]elseif[/URL] [COLOR= #007700]([/COLOR][COLOR= #0000BB]$gebruiker[/COLOR] [COLOR= #007700]!==[/COLOR] [COLOR= #0000BB]$wachtwoord[/COLOR][COLOR= #007700]){[/COLOR]
        
        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]<font color="#FF0000">De ingevulde gegevens zijn niet correct.</font>[/COLOR][COLOR= #007700];    

        }[/COLOR]

?>


<form action="" method="post">
   <table border="0">
      <tr>
         <td><input type="text" name="naam" value="Gebruikersnaam" size="20px"></td>
      </tr>
      <tr>
         <td><input type="password" name="pass" value="Wachtwoord" size="20px"></td>
      </tr>
      <tr>
         <td><input type="submit" name="inloggen" value="Inloggen"></td>
      </tr>
   </table>
</form>

</body>
</html>


Gebr.naam= test
Pass = demo

Alvast bedankt,

Jos
0
Berichten
2.517
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Bedoel je dat als je inlocht dat je naar andere pagina gaat?
0
Josnl

Lid · Topicstarter

Berichten
3.461
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Nee, dat als de inloggegevens kloppen, dat ie dan een sessie aanmaakt zodat je de paginas kan beveiligen sessie check. Ik weet alleen niet hoe ik zon sessie moet aanmaken...sadley
PS:
Hier staat het script: www.onehosted.nl/klanten/inloggen.php
0
Berichten
5.795
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Setcookie("sesion naam", $_POST[naam], Time()+3600*24*7);
0
Berichten
3.788
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Sessie aanmaken gaat heel makkelijk:

[COLOR= #0000BB]<?php
$_SESSION[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]sessienaam[/COLOR][COLOR= #007700]] =[/COLOR] [COLOR= #DD0000]waarde[/COLOR][COLOR= #007700];[/COLOR]
?>


Wel moet je erbij opletten dat bovenaan de pagina session_start() staat!
Dus in dit geval:

[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/session_start"]session_start[/URL][COLOR= #007700]();[/COLOR]
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Inloggen</title>
</head>

<body>


[COLOR= #0000BB]<?php


$gebruiker[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$_POST[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]naam[/COLOR][COLOR= #007700]];[/COLOR]
[COLOR= #0000BB]$wachtwoord[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$_POST[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]pass[/COLOR][COLOR= #007700]];[/COLOR]

[URL="http://www.php.net/if"]if[/URL] [COLOR= #007700]([/COLOR][COLOR= #0000BB]$gebruiker[/COLOR] [COLOR= #007700]===[/COLOR] test [COLOR= #007700]&&[/COLOR] [COLOR= #0000BB]$wachtwoord[/COLOR] [COLOR= #007700]===[/COLOR] demo[COLOR= #007700]){[/COLOR]

        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]<font color="#009900">Je bent succesvol ingelogd!</font>[/COLOR][COLOR= #007700];[/COLOR]
       [COLOR= #0000BB]$_SESSION[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]login[/COLOR][COLOR= #007700]] =[/COLOR] true[COLOR= #007700];
        
        }[/COLOR]
    
        [URL="http://www.php.net/elseif"]elseif[/URL] [COLOR= #007700]([/COLOR][COLOR= #0000BB]$gebruiker[/COLOR] [COLOR= #007700]!==[/COLOR] [COLOR= #0000BB]$wachtwoord[/COLOR][COLOR= #007700]){[/COLOR]
        
        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]<font color="#FF0000">De ingevulde gegevens zijn niet correct.</font>[/COLOR][COLOR= #007700];    

        }[/COLOR]

?>


<form action="" method="post">
   <table border="0">
      <tr>
         <td><input type="text" name="naam" value="Gebruikersnaam" size="20px"></td>
      </tr>
      <tr>
         <td><input type="password" name="pass" value="Wachtwoord" size="20px"></td>
      </tr>
      <tr>
         <td><input type="submit" name="inloggen" value="Inloggen"></td>
      </tr>
   </table>
</form>

</body>
</html>
0
Josnl

Lid · Topicstarter

Berichten
3.461
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Bedankt Daiman en Eleky, al wist ik dat van session_start(); bovenaan al. bloosNu maar even proberen of het lukt! yummie
Hoe controleer ik op een andere pagina of er een cookie is aangemaakt? bloos
0
Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
en op andere paginas doe je het zo:

[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/session_start"]session_start[/URL][COLOR= #007700]();[/COLOR]
[COLOR= #FF8000]// als je includes hebt hier instoppen
//layout bovenkant[/COLOR]
[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/isset"]isset[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_SESSION[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]login[/COLOR][COLOR= #007700]])){[/COLOR]
[URL="http://www.php.net/echo"]echo[/URL][COLOR= #DD0000]ingelogd[/COLOR][COLOR= #007700];
}[/COLOR][URL="http://www.php.net/else"]else[/URL][COLOR= #007700]{[/COLOR]
[URL="http://www.php.net/echo"]echo[/URL][COLOR= #DD0000]inloggen jij![/COLOR][COLOR= #007700];
}[/COLOR]
[COLOR= #FF8000]// onderkant[/COLOR]
?>


EDIT:
in je if van dit:
$gebruiker === test && $wachtwoord === demo
dit maken:
$gebruiker == test && $wachtwoord == demo
(staat netter)
0
Josnl

Lid · Topicstarter

Berichten
3.461
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
@Nielsie,

Waarom is dat netter dan? yummieHet werkt toch? swhip
EDIT:

Weet iemand waarom dit niet werkt?

[COLOR= #0000BB]<?php[/COLOR]

[COLOR= #FF8000]//Gebruikersnaam + Wachtwoord in variabele zetten[/COLOR]
[COLOR= #0000BB]$gebruiker[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$_POST[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]naam[/COLOR][COLOR= #007700]];[/COLOR]
[COLOR= #0000BB]$wachtwoord[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$_POST[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]pass[/COLOR][COLOR= #007700]];[/COLOR]


[COLOR= #FF8000]//Controleren of $gebruiker gelijk is aan test, en $wachtwoord gelijk is aan demo[/COLOR]
[URL="http://www.php.net/if"]if[/URL] [COLOR= #007700]([/COLOR][COLOR= #0000BB]$gebruiker[/COLOR] [COLOR= #007700]===[/COLOR] test [COLOR= #007700]&&[/COLOR] [COLOR= #0000BB]$wachtwoord[/COLOR] [COLOR= #007700]===[/COLOR] demo[COLOR= #007700]){[/COLOR]
    

[COLOR= #FF8000]//Als het klopt: cookie aanmaken, en weergeven dat je succesvol bent ingelogd    [/COLOR]
[URL="http://www.php.net/setcookie"]setcookie[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"ingelogd"[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$_POST[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]naam[/COLOR][COLOR= #007700]],[/COLOR] [URL="http://www.php.net/Time"]Time[/URL][COLOR= #007700]()+[/COLOR][COLOR= #0000BB]3600[/COLOR][COLOR= #007700]*[/COLOR][COLOR= #0000BB]24[/COLOR][COLOR= #007700]*[/COLOR][COLOR= #0000BB]7[/COLOR][COLOR= #007700]);[/COLOR]

        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]<font color="#009900">Je bent succesvol ingelogd!</font>[/COLOR][COLOR= #007700];[/COLOR]
        [URL="http://www.php.net/header"]header[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]location: www.onehosted.nl/klanten/admin.php[/COLOR][COLOR= #007700]);
        
        }[/COLOR]
        

[COLOR= #FF8000]//Als de gegevens niet kloppen echoen: De ingevulde gegevens zijn niet correct    [/COLOR]
        [URL="http://www.php.net/elseif"]elseif[/URL] [COLOR= #007700]([/COLOR][COLOR= #0000BB]$gebruiker[/COLOR] [COLOR= #007700]!==[/COLOR] [COLOR= #0000BB]$wachtwoord[/COLOR][COLOR= #007700]){[/COLOR]
        
        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]<font color="#FF0000">De ingevulde gegevens zijn niet correct.</font>[/COLOR][COLOR= #007700];    

        }[/COLOR]        

?>


Ik krijg de volgende error:

Warning: Cannot modify header information - headers already sent by (output started at /home/onehosted/domains/onehosted.nl/public_html/klanten/inloggen2.php:11) in /home/onehosted/domains/onehosted.nl/public_html/klanten/inloggen2.php on line 23
Je bent succesvol ingelogd!
Warning: Cannot modify header information - headers already sent by (output started at /home/onehosted/domains/onehosted.nl/public_html/klanten/inloggen2.php:11) in /home/onehosted/domains/onehosted.nl/public_html/klanten/inloggen2.php on line 26


Alvast bedankt!
0
Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
session_start();
0
Josnl

Lid · Topicstarter

Berichten
3.461
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Staat bovenaan de pagina, ik zal wel even de hele pagina hier posten:

[COLOR= #0000BB]<?php[/COLOR] [URL="http://www.php.net/session_start"]session_start[/URL][COLOR= #007700]();[/COLOR] ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Inloggen</title>
</head>

<body>


[COLOR= #0000BB]<?php[/COLOR]

[COLOR= #FF8000]//Gebruikersnaam + Wachtwoord in variabele zetten[/COLOR]
[COLOR= #0000BB]$gebruiker[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$_POST[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]naam[/COLOR][COLOR= #007700]];[/COLOR]
[COLOR= #0000BB]$wachtwoord[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$_POST[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]pass[/COLOR][COLOR= #007700]];[/COLOR]


[COLOR= #FF8000]//Controleren of $gebruiker gelijk is aan test, en $wachtwoord gelijk is aan demo[/COLOR]
[URL="http://www.php.net/if"]if[/URL] [COLOR= #007700]([/COLOR][COLOR= #0000BB]$gebruiker[/COLOR] [COLOR= #007700]===[/COLOR] test [COLOR= #007700]&&[/COLOR] [COLOR= #0000BB]$wachtwoord[/COLOR] [COLOR= #007700]===[/COLOR] demo[COLOR= #007700]){[/COLOR]
    

[COLOR= #FF8000]//Als het klopt: cookie aanmaken, en weergeven dat je succesvol bent ingelogd    [/COLOR]
[URL="http://www.php.net/setcookie"]setcookie[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"ingelogd"[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$_POST[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]naam[/COLOR][COLOR= #007700]],[/COLOR] [URL="http://www.php.net/Time"]Time[/URL][COLOR= #007700]()+[/COLOR][COLOR= #0000BB]3600[/COLOR][COLOR= #007700]*[/COLOR][COLOR= #0000BB]24[/COLOR][COLOR= #007700]*[/COLOR][COLOR= #0000BB]7[/COLOR][COLOR= #007700]);[/COLOR]

        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]<font color="#009900">Je bent succesvol ingelogd!</font>[/COLOR][COLOR= #007700];[/COLOR]
        [URL="http://www.php.net/header"]header[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]location: www.onehosted.nl/klanten/admin.php[/COLOR][COLOR= #007700]);
        
        }[/COLOR]
        

[COLOR= #FF8000]//Als de gegevens niet kloppen echoen: De ingevulde gegevens zijn niet correct    [/COLOR]
        [URL="http://www.php.net/elseif"]elseif[/URL] [COLOR= #007700]([/COLOR][COLOR= #0000BB]$gebruiker[/COLOR] [COLOR= #007700]!==[/COLOR] [COLOR= #0000BB]$wachtwoord[/COLOR][COLOR= #007700]){[/COLOR]
        
        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]<font color="#FF0000">De ingevulde gegevens zijn niet correct.</font>[/COLOR][COLOR= #007700];    

        }[/COLOR]        

?>


<form action="" method="post">
   <table border="0">
      <tr>
         <td><input type="text" name="naam" value="Gebruikersnaam" size="20px"></td>
      </tr>
      <tr>
         <td><input type="password" name="pass" value="Wachtwoord" size="20px"></td>
      </tr>
      <tr>
         <td><input type="submit" name="inloggen" value="Inloggen"></td>
      </tr>
   </table>
</form>

</body>
</html>


Het heeft denk ik met die header(location: oihohw) te maken...

EDIT:
Opgelost, moest ob_start(); toevoegen onder session_start();
0
Berichten
3.788
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Je kan geen cookies aanmaken midden in de code, dit moet ook bovenaan de pagina.
Hetzelfde geld voor header();
0
Josnl

Lid · Topicstarter

Berichten
3.461
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Maar hij moet de cookie alleen aanmaken als de goede gegevens zijn ingevuld, dus als ik het bovenaan zet dan maakt ie toch altijd een cookie aan?confused
0
Berichten
3.788
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Nee, je moet de php code bovenaan zetten, gewoon de gewone code!
0

Jouw reactie

Deel je idee, stel een vraag of help een ander verder.

Editor laden…

Praat mee met de One2xs-community.

Inloggen om te reageren