[PHP] Problem mit Cookies

Dieses Thema im Forum "Webentwicklung" wurde erstellt von scriptkiddy, 14. Februar 2007 .

Schlagworte:
  1. 14. Februar 2007
    Problem mit Cookies

    Hallo!
    Habe mal engefangen ein kleines Login zu schreiben. Das eigentliche Login klappt auch nur möchte ich jetzt Cookies mit einbringen:
    index.php
    Code:
    <? session_start(); ?>
    <!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=iso-8859-1" />
    <title>index1</title>
    </head>
    
    <body>
    <table width="100%" border="0">
     <tr>
     <td><table width="100%" border="0">
     <tr>
     <td width="10%"><table width="100%" border="0">
     <tr>
     <td>l_head</td>
     </tr>
     <tr>
     <td>l_body</td>
     </tr>
     <tr>
     <td>l_foot</td>
     </tr>
     </table></td>
     <td width="80%"><table width="100%" border="0">
     <tr>
     <td><h2><center>Das ist eine ganz tolle Testseite zu PROJECT1</center></h2></td>
     </tr>
     <tr>
     <td><table width="100%" border="0">
     <tr>
     <td>
     <?php
     if(isset($_GET['site']) && preg_match('%^[a-z0-9_-]+$%i',$_GET['site']) && file_exists('./'.$_GET['site'].'.php')) 
     {
     include('./'.$_GET['site'].'.php');
     } else {
     include('./welcome.php');
     }
     ?>
     </td>
     </tr>
     </table></td>
     </tr>
     <tr>
     <td><?php if($_SESSION['pro1_uname']) { print("happy"); } ?></td>
     </tr>
     </table></td>
     <td width="10%"><table width="100%" border="0">
     <tr>
     <td>Login:</td>
     </tr>
     <tr>
     <td><form name="login" method="post" action="index.php?site=login">
     <input type="text" name="username"><br>
     <input type="password" name="password"><br />
     <input type="checkbox" name="cookie"/><small>Auto-Login?</small><br /><br />
     <input type="submit" value="Send">
     <input type="reset" value="Reset">
     </form>
     </td>
     </tr>
     <tr>
     <td>r_foot</td>
     </tr>
     </table></td>
     </tr>
     </table></td>
     </tr>
    </table>
    </body>
    </html>
    login.php
    Code:
    <html>
    <head>
    <meta http-equiv="refresh" content="5;URL=javascript:history.back();" />
    </head>
    </html>
    <?
    
    
    
    include "secure.php";
    include "mysql_config.php";
    
    $back = "Sie werden in 5 Sekunden weitergeleitet! Falls nicht, klicken sie <a href='javascript:history.back();'>hier</a>!";
    
    $user = $_POST['username'];
    $pwd = $_POST['password'];
    $cookie = $_POST['cookie'];
    
    if($user == "" || $pwd == "")
    {
     $error = "<p><b>Keine korrekte Eingabe!</p></b>";
    } 
    else 
    {
     if(!checkinput($user) == true && !checkinput($pwd) == true) 
     {
    
     $npwd = md5($pwd);
    
     $db = mysql_connect($mysql_server,$mysql_user,$mysql_passwd);
     mysql_select_db($mysql_dbname);
    
     $query = mysql_query("SELECT user,pwd FROM users WHERE user = '$user'");
     list($check_user,$check_pwd) = mysql_fetch_row($query);
     
     if($check_user == $user && $check_pwd == $npwd)
     {
     if(isset($cookie))
     {
     setcookie(pro1_uname,$user,time()+999999);
     setcookie(pro1_pwd,$npwd,time()+999999); 
     }
     $error = "<p><b>Ihre Daten waren Korrekt, Sie werden weitergeleitet.</p></b>";
     } 
     else
     { 
     $error = "<p><b>Userdaten nicht gefunden!</p></b>";
     } 
     }
     else 
     {
     $error = "<p><b>Ihre Eingabe enthält ungültige Zeichen!</p></b>";
     }
    } 
    print($error);
    print($back);
    
    ?>
    Wenn ich mich jetzt korrekt einloge kommt folgende Fehlermeldung:
    Code:
    Warning: Cannot modify header information - headers already sent by (output started at D:\**\index.php:14) in D:\**\login.php on line 40
    
    Warning: Cannot modify header information - headers already sent by (output started at D:\**\index.php:14) in D:\**\login.php on line 41
    Ich weiß absolut nicht was falsch daran ist und bei google etc. hab ich leider auch noch nichts gefunden.

    Wäre nett wenns mir jemand erklären könnte.
    MfG scriptkiddy
     
  2. 14. Februar 2007
    AW: Problem mit Cookies

    Du darfst vor session_start() keinen Output senden. session_start() muss noch Header senden können, das kann es aber nicht, wenn bereits der Body gesendet wird/wurde.


    Steht aber auch in der Fehlermeldung, also RTFEM!


    Und das ist ja wohl mal glatt eine Lüge!
    Ich kriege, wenn ich nach "Cannot modify header information - headers already sent" suche, 1.41 Millionen Ergebnisse, also erzähl mir nichts.
     
  3. 14. Februar 2007
    AW: Problem mit Cookies

    JO und da steht bei den meisten was von header funktion bei php.. die hab ich allerdings nicht benutzt?!

    aber danke trotzdem
     
  4. 14. Februar 2007
    AW: Problem mit Cookies

    Poste mal den code von secure.php und mysql_config.php. Vielleicht haste da ein
    <? session_start(); ?>
    drin, was eigentlich an den Anfang gehört.


    mfg,
    m00pd00p
     
  5. 14. Februar 2007
    AW: Problem mit Cookies

    Ist irrelevant, steht aber auch in der Fehlermeldung.
     
  6. 14. Februar 2007
    AW: Problem mit Cookies

    ist vllt ein bisschen performance-lastig, aber schreib mal ganz an den anfang ein ob_start()
    damit sollte dein problem schnells möglich gelöst sein
     
  7. 14. Februar 2007
    AW: Problem mit Cookies

    Eigentlich nicht unbedingt.
    Die ganzen kleinen echo()s fressen auch Performance.
     
  8. 16. Februar 2007
    AW: Problem mit Cookies

    setcookie() ist übrigens auch sone nette header-problem-funktion. davor niemals output rauslassen, sonst kannst dus vergessen! (<html> oder so zählt dazu acuh )

    wenn du das nicht hinkriegst ist das pech ... bzw solltest du noch ein wenig üben
     
  9. Video Script

    Videos zum Themenbereich

    * gefundene Videos auf YouTube, anhand der Überschrift.