By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,829 Members | 1,798 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,829 IT Pros & Developers. It's quick & easy.

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

P: 14
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”’);header(‘HTTP/1.0 401 Unauthorized’);exit(“This page requires authentication!”);}
// Testing the user name and password entered by the user
else{
include(“Vars.inc”);
$user_name = trim($_SERVER[‘PHP_AUTH_USER’]);
$user_password = trim($_SERVER[‘PHP_AUTH_PW’]);
$connection = mysql_connect($host,$user,$password)or die (“Couldn’t connect to server.”);$db = mysql_select_db($database,$connection)or die (“Couldn’t select database.”);$sql = “SELECT user_name FROM Valid_User WHERE user_name = ‘$user_name’AND password = md5(‘$user_password’)”;$result = mysql_query($sql)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(“imagegallery.inc”);
?>
Dec 13 '07 #1
Share this Question
Share on Google+
5 Replies


100+
P: 118
In your code, your if statement is part of the comment. That is why it does not work.

Sam
Dec 13 '07 #2

P: 14
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 :(
Dec 14 '07 #3

P: 14
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($_SERVER[‘PHP_AUTH_USER’]))
{
header(‘WWW-Authenticate: Basic realm=”Image gallery”’);
header(‘HTTP/1.0 401 Unauthorized’);
exit(“This page requires authentication!”);}

else{
include(“Vars.inc”);
$user_name = trim($_SERVER[‘PHP_AUTH_USER’]);
$user_password = trim($_SERVER[‘PHP_AUTH_PW’]);
$connection = mysql_connect($host,$user,$password)or die (“Couldn’t connect to server.”);
$db = mysql_select_db($database,$connection)or die (“Couldn’t select database.”);
$sql = “SELECT user_name FROM Valid_User WHERE user_name = ‘$user_name’AND password = md5(‘$user_password’)”;
$result = mysql_query($sql)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(“imagegallery.inc”);
?>
Dec 16 '07 #4

100+
P: 118
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:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /* Program: Auth.php
  3. * Desc:    Program that prompts for a user name and 
  4. *          password from the user using HTTP authentication.
  5. *          The program then tests whether the user
  6. *          name and password match a user name and password 
  7. *          pair stored in a MySQL database.
  8. */
  9.  
  10. if(!isset($_SERVER[‘PHP_AUTH_USER’]))
  11. {
  12. header(‘WWW-Authenticate: Basic realm=”Image gallery”’);
  13. header(‘HTTP/1.0 401 Unauthorized’);
  14. exit(“This page requires authentication!”);}
  15.  
  16. else{
  17. include(“Vars.inc”);
  18. $user_name = trim($_SERVER[‘PHP_AUTH_USER’]);
  19. $user_password = trim($_SERVER[‘PHP_AUTH_PW’]);
  20. $connection = mysql_connect($host,$user,$password)or die (“Couldn’t connect to server.”);
  21. $db = mysql_select_db($database,$connection)or die (“Couldn’t select database.”);
  22. $sql = “SELECT user_name FROM Valid_User WHERE user_name = ‘$user_name’AND password = md5(‘$user_password’)”;
  23. $result = mysql_query($sql)or die(“Couldn’t execute query.”);
  24. $num = mysql_num_rows($result);
  25. if ($num < 1)  
  26. {
  27. exit(“The User Name or Password you entered is not valid.<br>”);
  28.  
  29. }
  30. }
  31. include(“imagegallery.inc”);
  32. ?>
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:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /* Program: Auth.php
  3. * Desc: Program that prompts for a user name and
  4. * password from the user using HTTP authentication.
  5. * The program then tests whether the user
  6. * name and password match a user name and password
  7. * pair stored in a MySQL database.
  8. */
  9.  
  10. if(!isset($_SERVER[‘PHP_AUTH_USER’]))
  11. {
  12. header("WWW-Authenticate: Basic realm=”Image gallery”");
  13. header("HTTP/1.0 401 Unauthorized");
  14. exit("This page requires authentication!");}
  15.  
  16. else{
  17. include(“Vars.inc”);
  18. $user_name = trim($_SERVER[‘PHP_AUTH_USER’]);
  19. $user_password = trim($_SERVER[‘PHP_AUTH_PW’]);
  20. $connection = mysql_connect($host,$user,$password)or die ("Couldn’t connect to server.");
  21. $db = mysql_select_db($database,$connection)or die ("Couldn’t select database.");
  22. $sql = "SELECT user_name FROM Valid_User WHERE user_name = ‘$user_name’AND password = md5(‘$user_password’)";
  23. $result = mysql_query($sql)or die("Couldn’t execute query.");
  24. $num = mysql_num_rows($result);
  25. if ($num < 1)
  26. {
  27. exit("The User Name or Password you entered is not valid.<br>");
  28.  
  29. }
  30. }
  31. include("imagegallery.inc");
  32. ?>
Sam
Dec 18 '07 #5

P: 14
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:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /* Program: Auth.php
  3. * Desc: Program that prompts for a user name and
  4. * password from the user using HTTP authentication.
  5. * The program then tests whether the user
  6. * name and password match a user name and password
  7. * pair stored in a MySQL database.
  8. */
  9.  
  10. if(!isset($_SERVER[‘PHP_AUTH_USER’]))
  11. {
  12. header("WWW-Authenticate: Basic realm=”Image gallery”");
  13. header("HTTP/1.0 401 Unauthorized");
  14. exit("This page requires authentication!");}
  15.  
  16. else{
  17. include(“Vars.inc”);
  18. $user_name = trim($_SERVER[‘PHP_AUTH_USER’]);
  19. $user_password = trim($_SERVER[‘PHP_AUTH_PW’]);
  20. $connection = mysql_connect($host,$user,$password)or die ("Couldn’t connect to server.");
  21. $db = mysql_select_db($database,$connection)or die ("Couldn’t select database.");
  22. $sql = "SELECT user_name FROM Valid_User WHERE user_name = ‘$user_name’AND password = md5(‘$user_password’)";
  23. $result = mysql_query($sql)or die("Couldn’t execute query.");
  24. $num = mysql_num_rows($result);
  25. if ($num < 1)
  26. {
  27. exit("The User Name or Password you entered is not valid.<br>");
  28.  
  29. }
  30. }
  31. include("imagegallery.inc");
  32. ?>
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 :)
Dec 19 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.