zeer simpele php login werkt niet...

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Jelle Sturm

Jelle Sturm

17/12/2008 21:26:00
Quote Anchor link
Hey,

Ik heb een simpel php login script maar ik weet niet hoe ik een sessie kan maken.

De actie van mijn form is login.php

login.php:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?php
ob_start();
include 'config.php';
$tbl_name="members"; // Table name

// Define $username and $password

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

// To protect MySQL injection (more detail about MySQL injection)
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);

$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $username and $password, table row must be 1 row

if($count==1){
// Register $username, $password and redirect to file "login_success.php"
session_register("username");
session_register("password");
header("location:login_success.php");
}

else {
echo "Wrong Username or Password";
}


ob_end_flush();
?>


Nu wil ik dat alleen ingelogde leden login_succes.php kunnen zien.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
<?php
session_start();
if($_SESSION['username'] == 'true'){
?>


<html>
<head>
<title>logged in</title>
</head>
<body>
Login Successful
</body>
</html>
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
<?

}
else{
 header('Location: index.php');
 }

?>


Ik dacht aan zoiets maar dit stuurt mij nadat ik ingelogt ben terug naar de index.
 
PHP hulp

PHP hulp

18/05/2024 05:21:08
 
Joren de Wit

Joren de Wit

17/12/2008 21:34:00
Quote Anchor link
Dat session_register() mag je vergeten, die functie is echt antiek. Een sessievariabele aanmaken gaat gewoon als volgt:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
<?php
$_SESSION
['username'] = 'pietje';
?>

Vergeet verder ook niet om elk script waarin je sessievariabelen gebruikt, te beginnen met session_start(). Anders zal het niet werken...
 
Jesper Diovo

Jesper Diovo

17/12/2008 22:28:00
Quote Anchor link
Zet session_start() ook boven je inlogpagina. Dan kun je daar ookal de sessies aanmaken. Dat doe je tegenwoordig gewoon met:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
<?php
$_SESSION
['naamvansessie'] = 'waarde van sessie';
?>
 
Joren de Wit

Joren de Wit

17/12/2008 23:11:00
Quote Anchor link
Is dat niet precies hetzelfde als ik ook al zei?
 
Jesper Diovo

Jesper Diovo

17/12/2008 23:13:00
Quote Anchor link
Blanche schreef op 17.12.2008 23:11:
Is dat niet precies hetzelfde als ik ook al zei?

Nu ik het nalees wel ja, ik had jouw post eerlijk gezegd niet doorgelezen, zag alleen dat je het over session_register() had :-P.
 
--

--

17/12/2008 23:49:00
Quote Anchor link
En ob_start() en ob_end_flush() ff wegkeilen.
 
Jelle Sturm

Jelle Sturm

18/12/2008 11:19:00
Quote Anchor link
Ik wordt nog steeds terug naar de index gestuurd omdat hij denk dat ik niet ben ingelogt...

login.php:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?php
ob_start();
include 'config.php';
$tbl_name="members"; // Table name

// Define $username and $password

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

// To protect MySQL injection (more detail about MySQL injection)
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);

$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $username and $password, table row must be 1 row

if($count==1){
// Register $username, $password and redirect to file "login_success.php"

$_SESSION['username'] = '$username';

header("location:login_success.php");
}

else {
echo "Wrong Username or Password";
}


ob_end_flush();
?>


login_succes.php
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
session_start();
if($_SESSION['username'] == 'true'){
?>


<html>
<head>
<title>logged in</title>
</head>
<body>
Login Successful
</body>
</html>
<?

}
else{
 header('Location: index.php');
 }

?>

Normaal zou ik login succesfull moeten zien maar hij gaat naar de else en stuurt me terug naar de index...
Gewijzigd op 01/01/1970 01:00:00 door Jelle Sturm
 
- SanThe -

- SanThe -

18/12/2008 11:26:00
Quote Anchor link
Pas je post even aan en gebruik en tags om je code heen. Om php code zet je uiteraard dit:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
<?php en ?>
Gebruik je Afbeelding knop om dit te doen.

Zet dit bovenin je script.
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

// rest
?>

SanThe.
 
Jelle Sturm

Jelle Sturm

18/12/2008 11:34:00
Quote Anchor link
^^Dat heeft geen nut omdat hij naar de else gaat is er ook geen echte fout. Ik word omgeleid naar de index.php.

Ik maak de sessie

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
$_SESSION['username'] = '$username';


als de username en pass overeenkomen met die in de database

in login_succes controleer ik die met

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
if($_SESSION['username'] == 'true'){


Wat doe ik fout?
 
- SanThe -

- SanThe -

18/12/2008 11:46:00
Quote Anchor link
Dit is fout:
$_SESSION['username'] = '$username';

Nooit een $var tussen enkele quotes zetten.
$_SESSION['username'] = $username;

Edit: En true ook nooit tussen quotes zetten.
Gewijzigd op 01/01/1970 01:00:00 door - SanThe -
 
Jelle Sturm

Jelle Sturm

18/12/2008 11:56:00
Quote Anchor link
Bedankt!!

het werkt nu... :D

Alleen als ik nu naar login_succes.php ga zonder in te loggen krijg ik een
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
Notice: Undefined index: username in /customers/buxonline.net/buxonline.net/httpd.www/login_success.php on line 7

Warning: Cannot modify header information - headers already sent by (output started at /customers/buxonline.net/buxonline.net/httpd.www/login_success.php:7) in /customers/buxonline.net/buxonline.net/httpd.www/login_success.php on line 22
fout.

login_succes.php:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

// rest
session_start();
if($_SESSION['username'] == true){
?>


<html>
<head>
<title>logged in</title>
</head>
<body>
Login Successful
</body>
</html>
<?

}
else{
 header('Location: index.php');
 }

?>


Zo belangrijk is dit niet, maar is er toch een oplossing om dan gewoon terug naar de index te sturen?
 
- SanThe -

- SanThe -

18/12/2008 12:01:00
Quote Anchor link
if($_SESSION['username'] == true){

Wordt:
if(isset($_SESSION['username']) and ($_SESSION['username'] == true)){
 
Jelle Sturm

Jelle Sturm

18/12/2008 12:10:00
Quote Anchor link
^^De fout is nu weg... maar het lukte daarjuist precies toch niet. hij maakt die sessie niet aan en nu denkt hij in login_succes.php dat die sessie er niet is.

Ik zie nu not allowed to be here als ik heb ingelogt...

Login.php
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

// rest
ob_start();
include 'config.php';
$tbl_name="members"; // Table name

// Define $username and $password

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

// To protect MySQL injection (more detail about MySQL injection)
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);

$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $username and $password, table row must be 1 row

if($count==1){
// Register $username, $password and redirect to file "login_success.php"
$_SESSION['username'] = $username;

header("location:login_success.php");
}

else {
echo "Wrong Username or Password";
}


ob_end_flush();
?>


Login_succes.php
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

// rest
session_start();
if(isset($_SESSION['username']) and ($_SESSION['username'] == true)){
?>


<html>
<head>
<title>logged in</title>
</head>
<body>
Login Successfull
<br />
<a href="logout.php">Logout!</a>
</body>
</html>
<?

}
else{
echo "Not allowed to be here";
 //header('Location: index.php');
 }
?>


Logout.php (heeft het hier mischien met te maken?)
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
<?
session_start();
session_destroy();
header('Location: index.php');
?>
 
- SanThe -

- SanThe -

18/12/2008 12:19:00
Quote Anchor link
In login.php zie ik geen session_start();
 
Jelle Sturm

Jelle Sturm

18/12/2008 12:27:00
Quote Anchor link
^^

Script werkt perfect!!

Danku!!
 
--

--

18/12/2008 13:23:00
Quote Anchor link
Evert schreef op 17.12.2008 23:49:
En ob_start() en ob_end_flush() ff wegkeilen.
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.