Results 1 to 4 of 4

Thread: plz help me

  1. #1
    Join Date
    May 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile plz help me

    I need code of login and remember me with cookies in php without using database because I have one user to login.

    I used many of codes but I had problem in method setcookie

    this is example of codes but can one tell is it true or not????

    this error appear when run it
    Warning: Cannot modify header information - headers already sent by ......


    index.php:


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <link rel = "stylesheet" type = "text/css"
    href="../externalCSS.css"/>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <?php
    /* These are our valid username and passwords */
    $user = 'test';
    $pass = 'test';
    if (isset($_COOKIE['username']) && isset($_COOKIE['password']))
    {

    if (($_POST['username'] != $user) || ($_POST['password'] != md5($pass)))
    {
    echo ('Location: newlogin.php');
    }
    else
    {
    echo 'Welcome back ' . $_COOKIE['username'];
    }

    }
    else
    {
    echo ('Location: newlogin.php');
    }
    ?>
    <form method="post" name="cookie" action="newlogin.php">
    <table align="center">
    <tr>
    <td ><sub><img src="../design images/logo.jpg" width="76" height="87"/></sub><b>Four Seasons</b></td>
    </tr>

    <tr><td><hr color="#CC0033"/></td></tr>
    <tr><td align="center">
    <table style= "border-style:solid;border-color:#CC0033;border-width:thin;align:center">
    <tr><td><label for="username">Username: </td><td><input name="username" t id="username" type="text" size="25" /></label></td></tr>
    <tr><td><label for="password">Password: </td><td><input name="password" id="password" type="password" size="25" /></label></td></tr>
    <tr><td><input type="checkbox" name="setcookie" value="setcookie" /> Remember Me</td></tr>
    <tr align="center"><td colspan="2"><input type="submit" name="submit" value="Submit" /><input type="reset" name="reset" value="Reset"/></td></tr></table>
    </td></tr>
    </form>

    <!--The follwing row is just for pushing the footer to the bottom-->
    <tr height="90"><td></td></tr>
    <tr><td><br /><hr color="#CC0033"/></td></tr>
    <tr><td align="center"><p class="smallFont">King Saud University. All rights reserved,2008</p></td></tr>
    </table>
    </body>
    </html>




    newlogin.php:


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <?php
    /* These are our valid username and passwords */
    $user = 'test';
    $pass = 'test';
    extract($_POST);
    if (isset($username) && isset($password)) {

    if (($username == $user) && ($password == $pass)) {

    if (isset($rememberme)) {
    /* Set cookie to last 1 year */
    setcookie('username', $username, time()+60*60*24*365, '', '');
    setcookie('password', md5($password), time()+60*60*24*365, '', '');

    } else {
    /* Cookie expires when browser closes */
    setcookie('username', $username, false, '', '');
    setcookie('password', md5($password), false, '', '');
    }
    print ("<h1>Registered!</h1><p>Thank you <b>
    <?php echo $username; ?> </b>, your information has been added to the database, you may now <a href='adminHome.htm' title='Login'>log in</a>.</p>
    ");

    } else {
    print ("Username/Password Invalid");
    die();
    }

    } else {
    print ("You must supply a username and password.");
    die();
    }
    ?>
    </body>
    </html>
    Last edited by Smile; 05-29-2008 at 03:46 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    I think that the echo ('Location: ...'); has to be in the <head> part of the document. And I think that it should be: header ('Location: ...'); from my knowledge at least. Also same with the cookies, in the <head> part of the document.
    Also just a tip:
    PHP Code:
    print ("Username/Password Invalid");
    die(); 
    Make it this:
    PHP Code:
    die("Username/Password Invalid"); 
    Same for the print() below it.
    Jeremy | jfein.net

  3. #3
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Nile is partly correct. The setcookie and header parts need to be placed before anything is outputted to the browser. Basically, you would need to do something like the following:

    index.php
    Code:
    <?php
    /* These are our valid username and passwords */
    $user = 'test';
    $pass = 'test';
    if (isset($_COOKIE['username']) && isset($_COOKIE['password'])) {
    
     if (($_POST['username'] != $user) || ($_POST['password'] != md5($pass))) {
      header('Location: newlogin.php');
     }
     else {
      $welcome = 'Welcome back ' . $_COOKIE['username'];
     }
    
    }
    
    else {
     header('Location: newlogin.php');
    }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <link rel = "stylesheet" type = "text/css"
    href="../externalCSS.css"/>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    
    <?php echo $welcome; ?>
    <form method="post" name="cookie" action="newlogin.php">
    <table align="center">
    <tr>
    <td ><sub><img src="../design images/logo.jpg" width="76" height="87"/></sub><b>Four Seasons</b></td>
    </tr>
    
    <tr><td><hr color="#CC0033"/></td></tr>
    <tr><td align="center">
    <table style= "border-style:solid;border-color:#CC0033;border-width:thin;align:center">
    <tr><td><label for="username">Username: </td><td><input name="username" t id="username" type="text" size="25" /></label></td></tr>
    <tr><td><label for="password">Password: </td><td><input name="password" id="password" type="password" size="25" /></label></td></tr>
    <tr><td><input type="checkbox" name="setcookie" value="setcookie" /> Remember Me</td></tr>
    <tr align="center"><td colspan="2"><input type="submit" name="submit" value="Submit" /><input type="reset" name="reset" value="Reset"/></td></tr></table>
    </td></tr>
    </form>
    
    <!--The follwing row is just for pushing the footer to the bottom-->
    <tr height="90"><td></td></tr>
    <tr><td><br /><hr color="#CC0033"/></td></tr>
    <tr><td align="center"><p class="smallFont">King Saud University. All rights reserved,2008</p></td></tr>
    </table>
    </body>
    </html>
    newlogin.php
    Code:
    <?php
    /* These are our valid username and passwords */
    $user = 'test';
    $pass = 'test';
    
    if (isset($_POST['username']) && isset($_POST['password'])) {
    
     if (($_POST['username'] == $user) && ($_POST['password'] == $pass)) {
    
      if (isset($_POST['rememberme'])) {
       /* Set cookie to last 1 year */
       setcookie('username', $username, time()+60*60*24*365, '', '');
       setcookie('password', md5($password), time()+60*60*24*365, '', '');
    
      } 
      else {
       /* Cookie expires when browser closes */
       setcookie('username', $username, false, '', '');
       setcookie('password', md5($password), false, '', '');
      }
    
    $body = '<h1>Registered!</h1><p>Thank you <b>'.$username.'</b>, your information has been added to the database, you may now <a href='adminHome.htm' title='Login'>log in</a>.</p>';
    
     } 
    
     else {
      $body = 'Username/Password Invalid';
     }
    
    } 
    
    else {
     $body = 'You must supply a username and password.';
    }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    
     <?php echo $body; ?>
    
    </body>
    </html>
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  4. #4
    Join Date
    May 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks alot for replying

    I found problem when change echo to header in
    header('Location: newlogin.php');
    because it is open newlogin.php before i enter the password and name...

    this the output
    "You must supply a username and password."

    and also code remember me doesn't work

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •