syntax error, unexpected T_ELSE in /home/allummfa/public_html/auth.php on line 10

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • goodguyjam
    New Member
    • Dec 2007
    • 14

    syntax error, unexpected T_ELSE in /home/allummfa/public_html/auth.php on line 10

    Hi I'm trying to apply user authentication with HTTP on my site but i get the above error. Can anyone say whats wrong?? I cant figure it out. Here's my code below:

    <?php
    /* Program: Auth.php
    * Desc: Program that prompts for a user name and
    * password from the user using HTTP authentication.
    * The program then tests whether the user
    * name and password match a user name and password
    * pair stored in a MySQL database.
    */
    //Testing whether the user has been prompted for a user nameif(!isset($ _SERVER[‘PHP_AUTH_USER’])){header(‘WWW-Authenticate: Basic realm=”secret section”’);head er(‘HTTP/1.0 401 Unauthorized’); exit(“This page requires authentication! ”);}
    // Testing the user name and password entered by the user
    else{
    include(“Vars.i nc”);
    $user_name = trim($_SERVER[‘PHP_AUTH_USER’]);
    $user_password = trim($_SERVER[‘PHP_AUTH_PW’]);
    $connection = mysql_connect($ host,$user,$pas sword)or die (“Couldn’t connect to server.”);$db = mysql_select_db ($database,$con nection)or die (“Couldn’t select database.”);$sq l = “SELECT user_name FROM Valid_User WHERE user_name = ‘$user_name’AND password = md5(‘$user_pass word’)”;$result = mysql_query($sq l)or die(“Couldn’t execute query.”);$num = mysql_num_rows( $result);if ($num < 1) // user name/password not found
    {
    exit(“The User Name or Password you entered is not valid.<br>”);

    }
    }
    //web page content.
    include(“imageg allery.inc”);
    ?>
  • helraizer1
    New Member
    • Mar 2007
    • 118

    #2
    In your code, your if statement is part of the comment. That is why it does not work.

    Sam

    Comment

    • goodguyjam
      New Member
      • Dec 2007
      • 14

      #3
      Originally posted by helraizer1
      In your code, your if statement is part of the comment. That is why it does not work.

      Sam
      thanks helraizer1, i'l give this a go...i should have spotted this :(

      Comment

      • goodguyjam
        New Member
        • Dec 2007
        • 14

        #4
        Originally posted by helraizer1
        In your code, your if statement is part of the comment. That is why it does not work.

        Sam

        Hi
        Thanks for this.
        I decided to remove most comments and re-arrange the line with the "if" script so that it all flows properly now.
        But now I get the error "Parse error: syntax error, unexpected ':' in /home/allummfa/public_html/auth.php on line 12".

        The only line in my code that uses the ":" is this line - header(‘WWW-Authenticate: Basic realm=”Image gallery”’);
        I did some research in google and found that this line is correctly written, including the ":". So this is confusing as I consistently get this error. All I want to do is get the first part of my coding to work which is to display the pop up window that prompts a user to enter their username and password and then i'l worry about the rest of the coding afterwards. I call the php script Auth.php and I have directed a link in my HTML code to this page, so that once a user clicks on the link, Auth.php should execute and the login prompt should display, at least thats how I believe it should work.
        Maybe if you test my php script here and see if it works for you......help.
        The re-arranged code is below:

        <?php
        /* Program: Auth.php
        * Desc: Program that prompts for a user name and
        * password from the user using HTTP authentication.
        * The program then tests whether the user
        * name and password match a user name and password
        * pair stored in a MySQL database.
        */

        if(!isset($_SER VER[‘PHP_AUTH_USER’]))
        {
        header(‘WWW-Authenticate: Basic realm=”Image gallery”’);
        header(‘HTTP/1.0 401 Unauthorized’);
        exit(“This page requires authentication! ”);}

        else{
        include(“Vars.i nc”);
        $user_name = trim($_SERVER[‘PHP_AUTH_USER’]);
        $user_password = trim($_SERVER[‘PHP_AUTH_PW’]);
        $connection = mysql_connect($ host,$user,$pas sword)or die (“Couldn’t connect to server.”);
        $db = mysql_select_db ($database,$con nection)or die (“Couldn’t select database.”);
        $sql = “SELECT user_name FROM Valid_User WHERE user_name = ‘$user_name’AND password = md5(‘$user_pass word’)”;
        $result = mysql_query($sq l)or die(“Couldn’t execute query.”);
        $num = mysql_num_rows( $result);
        if ($num < 1)
        {
        exit(“The User Name or Password you entered is not valid.<br>”);

        }
        }
        include(“imageg allery.inc”);
        ?>

        Comment

        • helraizer1
          New Member
          • Mar 2007
          • 118

          #5
          Originally posted by goodguyjam
          Hi
          Thanks for this.
          I decided to remove most comments and re-arrange the line with the "if" script so that it all flows properly now.
          But now I get the error "Parse error: syntax error, unexpected ':' in /home/allummfa/public_html/auth.php on line 12".

          The only line in my code that uses the ":" is this line - header(‘WWW-Authenticate: Basic realm=”Image gallery”’);
          I did some research in google and found that this line is correctly written, including the ":". So this is confusing as I consistently get this error. All I want to do is get the first part of my coding to work which is to display the pop up window that prompts a user to enter their username and password and then i'l worry about the rest of the coding afterwards. I call the php script Auth.php and I have directed a link in my HTML code to this page, so that once a user clicks on the link, Auth.php should execute and the login prompt should display, at least thats how I believe it should work.
          Maybe if you test my php script here and see if it works for you......help.
          The re-arranged code is below:

          [code=php]<?php
          /* Program: Auth.php
          * Desc: Program that prompts for a user name and
          * password from the user using HTTP authentication.
          * The program then tests whether the user
          * name and password match a user name and password
          * pair stored in a MySQL database.
          */

          if(!isset($_SER VER[‘PHP_AUTH_USER’]))
          {
          header(‘WWW-Authenticate: Basic realm=”Image gallery”’);
          header(‘HTTP/1.0 401 Unauthorized’);
          exit(“This page requires authentication! ”);}

          else{
          include(“Vars.i nc”);
          $user_name = trim($_SERVER[‘PHP_AUTH_USER’]);
          $user_password = trim($_SERVER[‘PHP_AUTH_PW’]);
          $connection = mysql_connect($ host,$user,$pas sword)or die (“Couldn’t connect to server.”);
          $db = mysql_select_db ($database,$con nection)or die (“Couldn’t select database.”);
          $sql = “SELECT user_name FROM Valid_User WHERE user_name = ‘$user_name’AND password = md5(‘$user_pass word’)”;
          $result = mysql_query($sq l)or die(“Couldn’t execute query.”);
          $num = mysql_num_rows( $result);
          if ($num < 1)
          {
          exit(“The User Name or Password you entered is not valid.<br>”);

          }
          }
          include(“imageg allery.inc”);
          ?>[/code]
          Hi Goodguyjam,

          I'm not sure how/why but you're using a different type of " and ' (double quotes and quotes). So it's technically not a string.

          use this:

          [code=php]
          <?php
          /* Program: Auth.php
          * Desc: Program that prompts for a user name and
          * password from the user using HTTP authentication.
          * The program then tests whether the user
          * name and password match a user name and password
          * pair stored in a MySQL database.
          */

          if(!isset($_SER VER[‘PHP_AUTH_USER’]))
          {
          header("WWW-Authenticate: Basic realm=”Image gallery”");
          header("HTTP/1.0 401 Unauthorized");
          exit("This page requires authentication! ");}

          else{
          include(“Vars.i nc”);
          $user_name = trim($_SERVER[‘PHP_AUTH_USER’]);
          $user_password = trim($_SERVER[‘PHP_AUTH_PW’]);
          $connection = mysql_connect($ host,$user,$pas sword)or die ("Couldn’t connect to server.");
          $db = mysql_select_db ($database,$con nection)or die ("Couldn’t select database.");
          $sql = "SELECT user_name FROM Valid_User WHERE user_name = ‘$user_name’AND password = md5(‘$user_pass word’)";
          $result = mysql_query($sq l)or die("Couldn’t execute query.");
          $num = mysql_num_rows( $result);
          if ($num < 1)
          {
          exit("The User Name or Password you entered is not valid.<br>");

          }
          }
          include("imageg allery.inc");
          ?>[/code]

          Sam

          Comment

          • goodguyjam
            New Member
            • Dec 2007
            • 14

            #6
            Originally posted by helraizer1
            Hi Goodguyjam,

            I'm not sure how/why but you're using a different type of " and ' (double quotes and quotes). So it's technically not a string.

            use this:

            [code=php]
            <?php
            /* Program: Auth.php
            * Desc: Program that prompts for a user name and
            * password from the user using HTTP authentication.
            * The program then tests whether the user
            * name and password match a user name and password
            * pair stored in a MySQL database.
            */

            if(!isset($_SER VER[‘PHP_AUTH_USER’]))
            {
            header("WWW-Authenticate: Basic realm=”Image gallery”");
            header("HTTP/1.0 401 Unauthorized");
            exit("This page requires authentication! ");}

            else{
            include(“Vars.i nc”);
            $user_name = trim($_SERVER[‘PHP_AUTH_USER’]);
            $user_password = trim($_SERVER[‘PHP_AUTH_PW’]);
            $connection = mysql_connect($ host,$user,$pas sword)or die ("Couldn’t connect to server.");
            $db = mysql_select_db ($database,$con nection)or die ("Couldn’t select database.");
            $sql = "SELECT user_name FROM Valid_User WHERE user_name = ‘$user_name’AND password = md5(‘$user_pass word’)";
            $result = mysql_query($sq l)or die("Couldn’t execute query.");
            $num = mysql_num_rows( $result);
            if ($num < 1)
            {
            exit("The User Name or Password you entered is not valid.<br>");

            }
            }
            include("imageg allery.inc");
            ?>[/code]

            Sam
            Hi and thanks for your response. I actually got this working already from advice from another member. I basically re-entered the quotation marks as I had copied the code from a php book i'm learning from and it appears that what was copied was not been read properly on the server. But stand by I may have more questions :)

            Comment

            Working...