| | | | |
| | |
| Gast
Beiträge: n/a
| Hola, ein ähnliches Thema wurde zwar schon öfters diskutiert, aber ein selbes Problem wie meines hab ich leider auch mit der Suche nicht gefunden. Nun, ersteinmal zu den Hintergründen meines Problems: Zuerst habe ich versucht, die Sessions _erstmal_so_ zum Laufen zu kriegen, bevor ich es in mein Projekt einbinde. Insofern hab ich auf alles überflüssige verzichtet. Es handelt sich um ein Session-Login, welche den Usernamen und das Passwort in einer MySQL-Datenbank abgleicht. Die Datenbank heißt "atciss_db", die Tabelle heißt "user" und darin sind enthalten: (id: 1 / username: admin / password: admin). Also in diesem Versuch hier funktioniert alles einwandtfrei: index.php Code: <?php session_start(); $_SESSION["auth"]=1; ?> <html> <head> <title>ATCISS</title> </head> <body> <center> <table width="98%" border="0" cellspacing="0"> <tr> <td><center>Authentifizierung</center></td> </tr> <tr> <td bgcolor="#050120"></td> </tr> <tr bgcolor="#4f0027"> <td> </td> </tr> <tr> <td bgcolor="#050120"</td> </tr> </table> </center> <center> <table width="98%" border="0" cellspacing="0"> <tr> <td> </td> </tr> <tr> <td> <center> <font size="3">Bitte authentifizieren Sie sich für diesen Bereich: </font> </center> <form name="UserLogin" method="post" action="login.php"> <input type="hidden" name="project" value="0"> <center> <table width="50%" border="0" cellspacing="0"> <tr> <td><font size="2"><div align="right">Username: </div></font></td> <td><input type="text" name="username"></td> </tr> <tr> <td><font size="2"><div align="right">Password: </div></font></td> <td><input type="password" name="password"></td> </tr> <tr> <td> </td> <td> <input type="submit" name="login" value="Login"></td> </tr> </table> </center> </form> </td> </tr> </table> </center> <center> <table width="98%" border="0" cellspacing="0"> <tr> <td bgcolor="#050120"></td> </tr> <tr bgcolor="#4f0027"> <td> </td> </tr> <tr> <td bgcolor="#050120"></td> </tr> <tr> <td> </td> </tr> </table> </body> </html> login.php Code: <?php
session_start();
if ($_SESSION["auth"]==1)
{
@mysql_connect("localhost", "root", "");
@mysql_select_db("atciss_db");
$result=@mysql_query("select * from user where username='".addslashes($_POST["username"])."' and password='".$_POST["password"]."'");
$row=mysql_fetch_array($result);
if($row["id"])
$_SESSION["auth"]=2;
else
echo "Username oder Passwort inkorrekt";
}
if($_SESSION["auth"]==0)
{
echo "Session existiert nicht";
}
if($_SESSION["auth"]==2)
{
header("Location:seite2.php");
}
?>
seite2.php Code: <?php
include('authheader.php');
?>
<html>
<head>
</head>
<body>
Das ist Seite 2
Logout
</body>
</html>
authheader.php Code: <?php
session_start();
if ($_SESSION["auth"]!=2)
{
header("Location:index.php");
exit;
}
?>
logout.php Code: <?php
session_start();
session_destroy();
?>
Sie sind ausgeloggt.
Das wird alles der Reihe nach aufgerufen und es funktioniert alles. Weiter im nächsten Post... |
| | |
| PHP Code Flüsterer Registriert seit: 21.08.2005 Beiträge: 4682 PHP-Kenntnisse: Fortgeschritten | |
| | |
| Gast
Beiträge: n/a
| Jetzt wollte ich dieses Konstrukt nehmen und in mein Projekt einbinden. Das Problem dabei ist, daß mein Projekt eine Menu-Navigation besitzt. Ähnlich, als wenn ich Frames benutze, habe ich die Hauptseite in 2 Bereiche aufgeteilt. Oben ist die Menu-Navigation und wenn ich dort auf einen Link klicke, wird unten in einer Tabelle die jeweilige Seite angezeigt. Ich habe das alles mal auf den minimalen Code reduziert: index.php Code: <?php error_reporting(E_ALL); session_start(); $_SESSION['auth']=1; ?> <html> <head> </head> <body> <center> <table width="100%" border="1" cellspacing="0"> <tr> <td> In dieser Tabelle wird die Menu-Navigation dargestellt: Link zum Login </td> </tr> <tr> <td> <?php if (empty($_GET['action'])) { include('news.php'); } else { switch($_GET['action']) { case 'login': include('unten.php'); break; case 'loginstart': include('login.php'); break; case 'seite2': include('seite2.php'); break; } } ?> </td> </tr> </table> </center> </body> </html> news.php ...wird aufgerufen, falls kein Link angeklickt ist (für die Vollständigkeit) Code: <html> <head> </head> <body> news </body> </html> unten.php ...wird aufgerufen, wenn der Link "Login" gedrückt wird Code: <html> <head> <title>ATCISS</title> </head> <body> Bitte authentifizieren Sie sich für diesen Bereich: </center> <form name="UserLogin" method="post" action="index.php?action=loginstart"> <input type="hidden" name="project" value="0"> Username: <input type="text" name="username"> Password: <input type="password" name="password"> <input type="submit" name="login" value="Login"> </form> </body> </html> Code: <?php
error_reporting(E_ALL);
// session_start();
if ($_SESSION["auth"]==1)
{
@mysql_connect("localhost", "root", "");
@mysql_select_db("atciss_db");
$result=@mysql_query("select * from user where username='".addslashes($_POST["username"])."' and password='".$_POST["password"]."'");
$row=mysql_fetch_array($result);
if($row["id"])
$_SESSION["auth"]=2;
else
echo "Username oder Passwort inkorrekt";
}
if($_SESSION["auth"]==0)
{
echo "Session existiert nicht";
}
if($_SESSION["auth"]==2)
{
header("Location: index.php?action=seite2"); // Hier liegt der Fehler mit der header-Funktion, genauso wie in der Datei authheader.php
exit; // Die seite2.php soll ebenfalls unterhalb des Menusystems angezeigt werden
}
?>
seite2.php Code: <?php
include('authheader.php');
?>
<html>
<head>
</head>
<body>
Das ist Seite 2
Logout
</body>
</html>
authheader.php Code: <?php
error_reporting(E_ALL);
// session_start();
if ($_SESSION["auth"]!=2)
{
header("Location: index.php"); // Hier besteht das selbe Problem wie bei der login.php
exit;
}
?>
logout.php Code: <?php
// session_start();
session_destroy();
?>
Sie sind ausgeloggt.
In den Dateien logout.php, authheader.php habe ich session_start(); als Kommentar gesetzt, da sonst Fehlermeldungen angezeigt werden. Außerdem bin ich mir nicht sicher, ob ich die Session nun in der index.php starten muß, oder aber in der login.php Ich bekomme da ständig die Fehler: Warning: Cannot modify header information - headers already sent by (output started at /www/ATCISS/projekt/session-test/index.php:22) in /www/ATCISS/projekt/session-test/login.php on line 25 Wenn ich die Funktion header(); aufrufe, möchte ich auf die index.php verweisen, welche dann die aufgerufene Datei unten anzeigt. Kann mir da bitte jemand helfen? Ich komm da einfach nicht weiter und im Netz hab ich weiter auch nichts gefunden, außer die üblichen Problembeschreibungen. Btw: Ich arbeite unter Linux Gruß und vielen Dank, BitByte |
| | |
| Gast
Beiträge: n/a
| http://www.php-faq.de/q/q-fehler-header.html analysier mal die genannten zeilen index.php:22 und login.php:25 auf die sachen wegen ausgabe & so späße. |
| | |
| Gast
Beiträge: n/a
| Ein Bekannter hat mir bei dem Problem geholfen (danke soox =) ...und es funktioniert nun. Für alle, die mal das selbe Problem haben,hier der funktionierende Code (Datenbank anlegen, wie oben beschrieben): index.php Code: <?php
session_start();
if (!isset($_SESSION['auth']))
{
$_SESSION['auth']=1;
}
?>
<html>
<head>
</head>
<body>
<center>
<table width="100%" border="1" cellspacing="0">
<tr>
<td>
In dieser Tabelle wird die Menu-Navigation dargestellt:
Link zum Login
</td>
</tr>
<tr>
<td>
<?php
if (empty($_GET['action']))
{
include('news.php');
} else {
switch($_GET['action'])
{
case 'login': include('unten.php'); break;
case 'seite2': include('seite2.php'); break;
}
}
?>
</td>
</tr>
</table>
</center>
</body>
</html>
news.php Code: news unten.php Code: <html>
<head>
<title>ATCISS</title>
</head>
<body>
Bitte authentifizieren Sie sich für diesen Bereich:
</center>
<?php
if (!empty($login_error) ) echo "<font color=red>".$login_error."</font>
\n";
{
?>
<form name="UserLogin" method="post" action="login.php">
<input type="hidden" name="project" value="0">
Username:
<input type="text" name="username">
Password:
<input type="password" name="password">
<input type="submit" name="login" value="Login">
</form>
<?php
}
?>
</body>
</html>
login.php Code: <?php
session_start();
if ($_SESSION["auth"]==1 AND !empty($_POST['username']) AND !empty($_POST['password'])) {
@mysql_connect("localhost", "root", "");
@mysql_select_db("atciss_db");
$result=@mysql_query("select * from user where username='".addslashes($_POST["username"])."' and password='".$_POST["password"]."'");
$row=mysql_fetch_array($result);
if($row["id"])
$_SESSION["auth"]=2;
else
header("Location: index.php?action=login&login_error=Username oder Passwort inkorrekt");
}
if($_SESSION["auth"]==0) {
header("Location: index.php?action=login&login_error=Session existiert nicht");
} elseif($_SESSION["auth"]==2) {
header("Location: index.php?action=seite2");
exit;
} else {
header("Location: index.php?action=login&login_error=unerwarteter error beim login");
}
?>
authheader.php Code: <?php
function is_auth() {
if ($_SESSION["auth"]!=2) {
include("unten.php");
return false;
} else {
return true;
}
}
?>
seite2.php Code: <?php
include('authheader.php');
if (is_auth() ) {
?>
Das ist Seite 2
Logout
<?php
}
?>
logout.php Code: <?php
session_start();
session_destroy();
?>
Sie sind ausgeloggt.
So long... Gruß BitByte |
| Themen-Optionen | |
| Thema bewerten | |
|
|
Ähnliche Themen | ||||
| Thema | Autor | Forum | Antworten | Letzter Beitrag |
| Download einer csv-Datei | netty | PHP-Fortgeschrittene | 6 | 02.05.2008 15:53 |
| https header und Cookie | TailerD | PHP Tipps 2008 | 5 | 26.01.2008 19:54 |
| header() ??? | TeazY | PHP Tipps 2008 | 9 | 11.01.2008 15:40 |
| problem bei sessions in komb. m. cookies. header umgehen? | Promaetheus | PHP Tipps 2006 | 10 | 02.05.2006 15:52 |
| Cookies &amp; Sessions. Seite funktioniert nur mit Reload | PHP-Fortgeschrittene | 4 | 18.09.2005 00:48 | |
| header() und Sessions | Stümper | PHP Tipps 2005 | 8 | 02.06.2005 20:12 |
| Problem: Cannot modify header information | Alpha Centauri | PHP Tipps 2005 | 11 | 25.04.2005 13:05 |
| header prob mit auth und smarty | PHP Tipps 2005 | 4 | 07.03.2005 12:10 | |
| [Erledigt] Datei selber nach einer Abfrage senden, header() geht nicht? | PHP Tipps 2005 | 2 | 23.02.2005 05:58 | |
| Redirect mit Header - Problem | PHP Tipps 2005 | 3 | 22.02.2005 10:28 | |
| Warning: Cannot modify header... einbinden nicht möglich? | PHP Tipps 2004 | 2 | 07.08.2004 15:06 | |
| ankommender http header in einem php script auslesen | PHP Tipps 2004 | 0 | 19.07.2004 23:37 | |
| Header Probleme! | Calexico | PHP Tipps 2004 | 7 | 30.06.2004 23:05 |
| Sessions, sessions und nochmal sessions | PHP-Fortgeschrittene | 0 | 06.06.2004 00:36 | |
| header | PHP Tipps 2004 | 4 | 03.06.2004 22:12 | |
| Besucher kamen über folgende Suchanfragen bei Google auf diese Seite |
| localhost a session had already been started, php include username password |

Dieser Inhalt ist unter einer Creative Commons-Lizenz lizenziert.