www.webdeveloper.com
Results 1 to 10 of 10

Thread: Syntax question

  1. #1
    Join Date
    Oct 2005
    Location
    Knoxville Tennessee
    Posts
    79

    Syntax question

    Php says I have an error in this script where I have noted it. Can anyone see the error here? Much appreciated

    Code:
    <?php
    
      session_start();
     // User is logging in
      if (isset($_POST["login"])){
      if (isset($_POST["username"]) && ($_POST["username"]
       == "******") && isset($_POST["password"])
       && ($_POST["password"]
        == "******"){  // php says this { should not be there, it looks right to me
          $_SESSION["Authenticated"] = 1;
      }
      else{
         $_SESSION["Authenticated"] = 0;
      }
      session_write_close();
      header("Location: protected.php");
      }
      // User is logging out
      if (isset($_GET["logout"])){
         session_destroy();
         header("Location: login.html");
      }
    
    ?>

  2. #2
    Join Date
    Aug 2003
    Location
    In my house
    Posts
    4,004
    You need another ) at the end...
    Code:
    <?php
    
      session_start();
     // User is logging in
      if (isset($_POST["login"])){
      if (isset($_POST["username"]) && ($_POST["username"]
       == "******") && isset($_POST["password"])
       && ($_POST["password"]
        == "******")){  // php says this { should not be there, it looks right to me
          $_SESSION["Authenticated"] = 1;
      }
      else{
         $_SESSION["Authenticated"] = 0;
      }
      session_write_close();
      header("Location: protected.php");
      }
      // User is logging out
      if (isset($_GET["logout"])){
         session_destroy();
         header("Location: login.html");
      }
    
    ?>

  3. #3
    Join Date
    Jun 2006
    Posts
    472
    you have one missing or too many if you use the [php] tags it will be in colour
    PHP Code:
    <?php

      session_start
    ();
     
    // User is logging in
      
    if (isset($_POST["login"]))
      {
          if (isset(
    $_POST["username"]) && ($_POST["username"]== "******") && isset($_POST["password"])
               && (
    $_POST["password"] == "******")
           {  
    // php says this  should not be there, it looks right to me
                  
    $_SESSION["Authenticated"] = 1;
           }
          else
          {
                 
    $_SESSION["Authenticated"] = 0;
             
    session_write_close();
              
    header("Location: protected.php");
          }
      }
      
    // User is logging out
      
    if (isset($_GET["logout"]))
      {
         
    session_destroy();
         
    header("Location: login.html");
      }
    ?>

  4. #4
    Join Date
    Oct 2005
    Location
    Knoxville Tennessee
    Posts
    79
    Thank you both, that did solve that problem. Of course it brought me to another. I get these errors

    Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by output started at /home/lunar/public_html/auth.php:11) in /home/lunar/public_html/auth.php on line 13


    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent

    Warning: Cannot modify header information - headers already sent by

    This is my first use of cookies and sessions, can you expalin briefly what has happend? Thanks again

  5. #5
    Join Date
    Aug 2006
    Posts
    162
    Is this your whole script?

    If not please post the rest of the script that comes above this part of your script.

  6. #6
    Join Date
    Oct 2005
    Location
    Knoxville Tennessee
    Posts
    79
    Yes, basically thats the whole thing. its just a script that is called when someone logs in from an html form that asks for name and password and consists of the same doctype and headers as the script below..

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
      <title></title>

    </head>

    <body>
     <?php

      session_start
    ();
     
    // User is logging in
      
    if (isset($_POST["login"])){
      if (isset(
    $_POST["username"]) && ($_POST["username"]
       == 
    "mcwilson") && isset($_POST["password"])
       && (
    $_POST["password"]
        == 
    "mcwilson")){
          
    $_SESSION["Authenticated"] = 1;
      }
      else{
         
    $_SESSION["Authenticated"] = 0;
      }
      
    session_write_close();
      
    header("Location: protected.php");
      }
      
    // User is logging out
      
    if (isset($_GET["logout"])){
         
    session_destroy();
         
    header("Location: login.html");
      }

    ?>
      </body>
    </html>

  7. #7
    Join Date
    Aug 2006
    Posts
    162
    Remove all of the following:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
    <head> 
      <title></title> 
    
    </head> 
    
    <body> 
    
      </body> 
    </html>
    Your script is trying to write html code to the browser and that meens that a header has already been sent to the browser.

    header("Location: login.html");
    always need to be the first header.

  8. #8
    Join Date
    Oct 2005
    Location
    Knoxville Tennessee
    Posts
    79
    Ah I understand, the php script is not a web page, its a script. I literally turrned it into something to be displayed(of course it wouldnt be). I should have thought that out, I just naturally started a new webpage when I did the script.

    Thank you so much.

  9. #9
    Join Date
    Oct 2005
    Location
    Knoxville Tennessee
    Posts
    79
    Well I thought I understood, I removed all headers and doc types from the script page, but it seemed to move the erorrs to the session start again.

    Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/lunar/public_html/auth.php:1) in /home/lunar/public_html/auth.php on line 2

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/lunar/public_html/auth.php:1) in /home/lunar/public_html/auth.php on line 2

    Warning: Cannot modify header information - headers already sent by (output started at /home/lunar/public_html/auth.php:1) in /home/lunar/public_html/auth.php on line 15


    Any last thoughts before I toss this out the window?

  10. #10
    Join Date
    Oct 2005
    Location
    Knoxville Tennessee
    Posts
    79
    At the point that Location: login.html occurs in the script, it is in response to a log out from the page that the script points to (protected.php), not in response from the initial login page.

    Does that make a difference?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles