problem mit header weiterleitung

Alles, was PHP betrifft, kann hier besprochen werden.

problem mit header weiterleitung

Postby 2Bad4You » 04. April 2008 17:39

Huhu,

Bei meinem Login script möchte ich nach erfolgreichem Login eine header Weiterleitung duchführen.
Also habe ich folgendes eingegeben:
Code: Select all
$_SESSION['user_id'] = $row['ID'];
$_SESSION['logged_in'] = TRUE;
header("Location:index.php?path=useracp.php$id=".$row['ID']."");


Jedoch kommt nun immer nach dem Login:
Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at /opt/lampp/htdocs/bad/index.php:22) in /opt/lampp/htdocs/bad/login/login.php on line 21


Aus der Fehlermeldung, already sent by ... index.php:22 habe ich gedacht die 22 stellt die Code Zeile da, in der der Fehler in der index.php sein soll. Das hier ist der Inhalt aus der index.php:
Code: Select all
<?php

if(!isset($_GET['path']))
{
   $OPEN = 'content/ipsum.html';
}
else
{
   $OPEN = $_GET['path'];
}
if(!file_exists($_GET['path']))
   {
    $OPEN = 'content/ipsum.html';
   }
?>
<html>
<head>
<title>2bad4You</title>
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico">
<link href="style/style.css" rel="stylesheet" type="text/css">
<link href="style/dropdown.css" rel="stylesheet" type="text/css">
<script language="JavaScript" src="style/dropdown.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<center>
<form action="index.php?path=login/login.php" method="post">
<table id="baddy" width="476" height="332" border="0" cellpadding="0" cellspacing="0">
   <tr>
    <td colspan="13" width="476" height="40"><table width="467" height="20" border="0">
    <tr>
    <td width="119" height="20" class="nr4" align="center"><input type="text" name="username" value="username" onBlur="<?php if(value ==''){?> value='username'<?php }?>" onFocus="<?php if(value =='username'){?>value=''<?php }?>" class="login"></td>
    <td width="2"></td>
    <td width="119" height="20" class="nr4" align="center"><input type="password" name="password" value="password" onBlur="<?php if(value ==''){?>value='password'<?php }?>" onFocus="<?php if(value=='password'){?>value=''<?php }?>"class="login"></td>
    <td width="2"></td>
    <td width="170" class="nr1"><input type="submit" value="Login" name="login" style="background:none; color:#FFFFFF; border:none; font-size:11px; font-weight:bold"> | <a href="index.php?path=login/register.php">register</a> | <a href="index.php?path=login/forgotpw.php">passwort?</a></td>
    </tr>
  </table>
  </form>
     </td>
   </tr>
   <tr>
   <td colspan="13"><img src="images/header.gif" width="476" height="153" alt="">   </td>
   </tr>
   <tr>
      <td colspan="13" width="476" height="9">         </td>
   </tr>
   <tr>
   <td colspan="13" background="images/index_04.gif" height="29">                               
        <ul id="nav">
            <li><img src="images/border01.jpg" />
            </li>
           <li>
            <a href="#" onMouseOver="link1.src='images/link1_over.gif';" onMouseOut="link1.src='images/link1_out.gif';" >
            <img name="link1" src="images/link1_out.gif" border="0"></a>
               <ul>
                  <li><a href="index.php?path=news/news.php">Startseite</a></li>
                   <li><a href="index.php?path=contact/contact.php">Kontakt</a></li>
                   <li><a href="index.php?path=gb/showgb.php">Gästebuch</a></li>
                   <li><a href="index.php?path=impressum/impressum.php">Impressum</a></li>
                </ul>
            </li>
            <li><img src="images/border01.jpg" />
            </li>
            <li>
            <a href="#" onMouseOver="link2.src='images/link2_over.gif';" onMouseOut="link2.src='images/link2_out.gif';" >
            <img name="link2" src="images/link2_out.gif" border="0"></a>
                <ul>
                    <li><a href="index.php?path=content/aboutme.php">Über Mich</a></li>
                    <li><a href="index.php?path=content/friends.php">Friend`s&nbsp;&nbsp;&nbsp;</a></li>
                    <li><a href="index.php?path=admingallery/gallerycats.php">Bilder&nbsp;&nbsp;&nbsp;</a></li>
                    <li><a href="index.php?path=content/gaming.php">Gaming</a></li>
                </ul>
            </li>
            <li><img src="images/border01.jpg" />
            </li>
            <li>
            <a href="#" onMouseOver="link3.src='images/link3_over.gif';" onMouseOut="link3.src='images/link3_out.gif';" >
            <img name="link3" src="images/link3_out.gif" border="0"></a>
                <ul>
                    <li><a href="index.php?path=content/hostinggames.php">Game Server</a></li>
                    <li><a href="index.php?path=content/hostingwebspace.php">Webspace</a></li>
                    <li><a href="index.php?path=content/hostingvoice.php">Voice Server</a></li>
                    <li><a href="index.php?path=content/testserver.php">Test Server</a></li>
                    <li><a href="index.php?path=content/Kundeninterface.php">Kunden Login</a></li>
                    <li><a href="index.php?path=content/support.php">Support&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a></li>
                    <li><a href="index.php?path=content/faq.php">FAQ</a></li>
                </ul>
            </li>
            <li><img src="images/border01.jpg" />
            </li>
            <li>
            <a href="blank.html" onMouseOver="link4.src='images/link4_over.gif';" onMouseOut="link4.src='images/link4_out.gif';" >
            <img name="link4" src="images/link4_out.gif" border="0"></a>           
            </li>
            <li><img src="images/border01.jpg" />
            </li>
            <li>
            <a href="blank.html" onMouseOver="link5.src='images/link5_over.gif';" onMouseOut="link5.src='images/link5_out.gif';" >
            <img name="link5" src="images/link5_out.gif" border="0"></a>           
            </li>
        </ul>   
      </td>
   </tr>
   <tr>
      <td colspan="13" width="476" height="8">
        </td>
   <tr>
      <td colspan="13" width="476" height="8">         </td>
   </tr>
   <tr>
      <td colspan="13" background="images/sub.gif" width="476" height="20" align="center"><table width="350" border="0">
  <tr>
    <td width="350" align="left" class="nr1"><a href="index.html">Subnavi</a> | <a href="index.html">Subnavi</a></td>
  </tr>
</table>     </td>
   </tr>
   <tr>
      <td colspan="13" width="476" height="10">         </td>
   </tr>
   <tr>
      <td width="476" height="16"></td>
      <td colspan="11" width="457" height="16" class="nr2"><?php include($OPEN); ?></td>
  <td width="9" height="16">         </td>
   </tr>
   <tr>
      <td colspan="13" background="images/foot.gif" width="476" height="23">         </td>
   </tr>
   <tr>
      <td colspan="13" width="476" height="7">         </td>
   </tr>
   <tr>
      <td colspan="13" background="images/footer.gif" width="476" height="30" align="center"><table width="440" border="0">
          <tr>
            <td width="300" class="nr3" align="left"><a href="index.html">Home</a> | <a href="about.html">About</a> | <a href="hosting.html">Hosting</a> | <a href="impressum.html">Impressum</a></td>
            <td width="140" class="nr3" align="center">Code &copy; 2Bad4You<br>Design &copy; <a href="http://www.spongeprodutions.com">sponge produtions</a></td></tr>
    </table></td>
   </tr>
   </table>
</center>
</body>
</html>


Liegt es daran dass die Weiterleitung nicht geht weil sozusagen alle Seiten includiert werden?
Wie kann man das umgehen bzw lösen?

Ansehen kann man das ganze hier:
hier
und dort einfach oben einmal mit "test" und als pw: "testuser" einloggen.

Danke im Vorraus
Last edited by 2Bad4You on 04. April 2008 23:51, edited 2 times in total.
2Bad4You
 
Posts: 15
Joined: 04. April 2007 17:02

Postby Wiedmann » 04. April 2008 17:55

Liegt es daran dass die Weiterleitung nicht geht weil sozusagen alle Seiten includiert werden?

Wahrscheinlich. Jedenfalls muss header() die erste Ausgabe sein. Und wie login.php aussieht, wissen wir ja nicht.
Wiedmann
AF Moderator
 
Posts: 17102
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

Postby 2Bad4You » 04. April 2008 18:02

achso hab ich vergessen den inhalt zu posten...also die login.php sieht so aus:

Code: Select all
<?php

require_once('inc/sqlconnect.inc.php');
include('inc/functions.inc.php');

if(isset($_POST['login']))
{
   if($_POST['username'] != '' || $_POST['password'] != '')
   {
      $query = mysql_query("SELECT ID, Username, Active FROM users WHERE
                       Username = '".mysql_real_escape_string($_POST['username'])."'
                       AND Password = '".mysql_real_escape_string(md5($_POST['password']))."'");
                      
      if(mysql_num_rows($query) == 1)
      {
         $row = mysql_fetch_assoc($query);
         if($row['Active'] == 1)
         {
            $_SESSION['user_id'] = $row['ID'];
            $_SESSION['logged_in'] = TRUE;
            header("Location:index.php?path=login/useracp.php&id=".$row['ID']."");
      }
         else
         {
            $error = ' Dein Account ist noch nicht aktiviert!<br>
                       Bitte benutze den Aktivierungslink, welchen du per Email bekommen hast, um deinen Account zu aktivieren.<br>
                     Falls du keine Aktivierungs Email bekommen hast, fordere hier eine neue an:<br>
                     <a href="../index.php?path=login/newkey.php">Neuen Link anfordern</a><br>';
         }
      }
      else
      {
         $error = ' Login fehlgeschlagen!<br>Es wurde kein Account gefunden mit diesem Benutzernamen oder die Kombination aus Benutzername/Passwort ist falsch!';
      }
   }
   else
   {
      $error = ' Du hast deinen Benutzernamen und/oder dein Passwort nicht eingegeben! ';
   }
}
if(isset($error))
{
   echo $error;
}
?>

2Bad4You
 
Posts: 15
Joined: 04. April 2007 17:02

Postby Wiedmann » 04. April 2008 18:07

Warning: Cannot modify header information - headers already sent by (output started at /opt/lampp/htdocs/bad/index.php:22) in /opt/lampp/htdocs/bad/login/login.php on line 21

Ich versee grad nur die Fehlermeldung nicht, da die "index.php" ja in der "login.php" gar nicht includiert wird (oder andersrum)?
Wiedmann
AF Moderator
 
Posts: 17102
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

Postby 2Bad4You » 04. April 2008 18:15

Also alle Seiten werden in der index.php includiert über
Code: Select all
path=../datei.php
also z.b
index.php?path=news/news.php
index.php?path=login/register.php

und dann wird das includierte script in der content tabelle ausgegeben:
Code: Select all
<td colspan="11" width="457" height="16" class="nr2"><?php include($OPEN); ?></td>


Hoffe das ist verständlich
2Bad4You
 
Posts: 15
Joined: 04. April 2007 17:02

Postby Wiedmann » 04. April 2008 18:30

und dann wird das includierte script in der content tabelle ausgegeben:

Du willst also sagen, bei dem was in "include $OPEN;" includiert wird, ist diese "login.php" auch dabei? Dann muss man sich ja nicht wundern, da alles vor diesem include ja eine Ausgabe an den Browser ist (die bei header nicht sein darf).
Wiedmann
AF Moderator
 
Posts: 17102
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

Postby 2Bad4You » 04. April 2008 19:45

hm gut... wie kann man dass denn anders machen? per meta weiterleitung? und dann gleich noch was:
Ich habe eine datei namens news.php in der einfach nur testweise dieses hier steht:
Code: Select all
<?php
session_start();

require_once('login/inc/sqlconnect.inc.php');
include('login/inc/functions.inc.php');

checkLogin('1 2');
echo "Logged in ^^";
?>

So nachdem mal eingeloggt ist sollte man diese Seite betreten können. Allerdings kommt dann immer:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /opt/lampp/htdocs/bad/index.php:22) in /opt/lampp/htdocs/bad/news/news.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /opt/lampp/htdocs/bad/index.php:22) in /opt/lampp/htdocs/bad/news/news.php on line 2

Warning: Cannot modify header information - headers already sent by (output started at /opt/lampp/htdocs/bad/index.php:22) in /opt/lampp/htdocs/bad/login/inc/functions.inc.php on line 26
Logged in ^^

Liegt das auch daran an dem includieren in der index.php?
Zu sehen hier

und hier noch schnell die functions.php, falls es fragen wegem dem level check gibt
Code: Select all
<?php
   function checkLogin($levels)
   {
      if(!$_SESSION['logged_in'])
      {
         $access = FALSE;
      }
      else {
         $kt = split(' ', $levels);
         
         $query = mysql_query('SELECT Level_access FROM users WHERE ID = "'.mysql_real_escape_string($_SESSION['user_id']).'"');
         $row = mysql_fetch_assoc($query);
         
         $access = FALSE;
         
         while(list($key,$val)=each($kt))
         {
            if($val==$row['Level_access'])
            {//if the user level matches one of the allowed levels
               $access = TRUE;
            }
         }
      }
      if($access==FALSE)
      {
         header("Location: login.php");
      }
      else {
      //do nothing: continue
      }
      
   }
   
   function valid_email($str)
   {
      return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
   }
   
   function checkUnique($field, $compared)
   {
      $query = mysql_query("SELECT `".mysql_real_escape_string($field)."` FROM `users` WHERE `".mysql_real_escape_string($field)."` = '".mysql_real_escape_string($compared)."'");
      if(mysql_num_rows($query)==0)
      {
         return TRUE;
      }
      else {
         return FALSE;
      }
   }
   
   function numeric($str)
   {
      return ( ! ereg("^[0-9\.]+$", $str)) ? FALSE : TRUE;
   }
   
   function alpha_numeric($str)
   {
      return ( ! preg_match("/^([-a-z0-9])+$/i", $str)) ? FALSE : TRUE;
   }
   
   function random_string($type = 'alnum', $len = 8)
   {               
      switch($type)
      {
         case 'alnum'   :
         case 'numeric'   :
         case 'nozero'   :
         
               switch ($type)
               {
                  case 'alnum'   :   $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                     break;
                  case 'numeric'   :   $pool = '0123456789';
                     break;
                  case 'nozero'   :   $pool = '123456789';
                     break;
               }
   
               $str = '';
               for ($i=0; $i < $len; $i++)
               {
                  $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
               }
               return $str;
           break;
         case 'unique' : return md5(uniqid(mt_rand()));
           break;
      }
   }
?>


Kann man das irgendwie anders regeln mit dem includieren bzw geht das mit Sessions dann überhaupt?

Danke schonmal für die bisherige Hilfe.
2Bad4You
 
Posts: 15
Joined: 04. April 2007 17:02

Postby Xardas der Dunkle » 04. April 2008 22:19

php.net wrote:Beachten Sie, dass Sie die Funktion header() aufrufen müssen, bevor Sie irgendeine andere Art von Ausgabe (seien es normale HTML-Tags, Leerzeilen in einer Datei oder von PHP) zum Client schicken. Es handelt sich hier um einen typischen Fehler, der zum Beispiel auftritt, wenn Sie Code mittels include() oder require() oder einer anderen Dateizugriffs-Funktion einlesen, die Leerzeichen oder Leerzeilen enthalten, die ausgegeben werden, bevor header() aufgerufen wird. Das gleiche Problem kann auch auftreten, wenn Sie eine Datei verwenden, in der HTML und PHP vermischt wurden.


Kurz du darfst keine Ausgabe haben bevor du eine Function ausführst die einen header sendet, sprich: header, setcookie, session_start, ...

Zudem ist es sinnfrei eine session nur auf der login seite zu starten :wink:
User avatar
Xardas der Dunkle
 
Posts: 482
Joined: 09. March 2008 19:40
Location: /var/www

Postby 2Bad4You » 04. April 2008 23:49

ah gut da bin ich schonmal weiter ^^ ...
habe jetzt ganz oben in die index.php
Code: Select all
session_start();

geschrieben. So wenn ich mich nun einlogge mache ich die Weiterleitung nun via
Code: Select all
echo '<meta http-equiv="refresh" content="2; URL=index.php?path=login/useracp.php">';

klappt auch soweit ganz gut ^^. Nun habe ich ja in der login.php die Session variablen mit der id gefüllt. So nun will ich das die Seite useracp.php, auf die weitergeleitet wird, nur sehen kann wenn man eingeloggt ist.
Also habe ich dort nun ganz oben
Code: Select all
require_once('inc/sqlconnect.inc.php');
include('inc/functions.inc.php');
checkLogin('1 2');

geschrieben. die Funktion check Login ist ja in der function.php folgendermaßen definiert:
Code: Select all
function checkLogin($levels)
   {
      if(!$_SESSION['logged_in'])
      {
         $access = FALSE;
      }
      else {
         $kt = split(' ', $levels);
         
         $query = mysql_query('SELECT Level_access FROM users WHERE ID = "'.mysql_real_escape_string($_SESSION['user_id']).'"');
         $row = mysql_fetch_assoc($query);
         
         $access = FALSE;
         
         while(list($key,$val)=each($kt))
         {
            if($val==$row['Level_access'])
            {//if the user level matches one of the allowed levels
               $access = TRUE;
            }
         }
      }
      if($access==FALSE)
      {
         echo '<meta http-equiv="refresh" content="1; URL="error.htm">';
      }
      else {
      //do nothing: continue
      }
      
   }


So nun kann ich aber ohne mich vorher eingeloggt zu haben die Seite useracp.php aufrufen.
Könnte das daran liegen dass die Weiterleitung wieder nur inkludiert ist, die Session aber in der index.php beginnt, diese also gar nicht aktualisiert wird und der daran nicht erkennt das in der Session Variable die ID und der status logged_in definiert ist?
Also ich sozusagen für die index.php gar nicht eingeloggt bin?

Zu sehen gibts das ganze <url=http://88.198.6.70/bad]Hier[/url]
user: test
pw: testuser

Hoffe ihr versteht was ich meine ^^

Und nochmal danle für die bisherige Hilfe und für die zukünftige. Echt klasse Forum
2Bad4You
 
Posts: 15
Joined: 04. April 2007 17:02

Postby Xardas der Dunkle » 05. April 2008 00:25

Wie wers wenn du eine Abfrage in die useracp.php setzt?

Code: Select all
if($_SESSION['logged_in'] === true) {
      echo 'Ich bin der interne Bereich.';
} else {
      echo 'Tut mir leid du hast keinen Zutritt.';
}


/edit: ich empfehle dir btw. die Verwendung von templates ..., dann kannst du auch wieder richtig weiterleiten ...
User avatar
Xardas der Dunkle
 
Posts: 482
Joined: 09. March 2008 19:40
Location: /var/www

Postby 2Bad4You » 06. April 2008 23:16

ok danke so hats dann nun fuktioniert :)
2Bad4You
 
Posts: 15
Joined: 04. April 2007 17:02


Return to PHP

Who is online

Users browsing this forum: No registered users and 12 guests