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

syntax error, unexpected ':' in /home/allummfa/public_html/auth.php on line 11

P: 14
Hi again. I now get the above error with the exact same code as in my previous question. All I did was to rearrange the lines...no code changes...help!
To assist you experts, line 11 contains this code - header(‘WWW-Authenticate: Basic realm=”secret section”’);
Then when I remove the colon, I get a new error - syntax error, unexpected T_STRING in /home/allummfa/public_html/auth.php on line 11.
This code is supposed to tell the web server to send the headers to the client's browser to prompt for a username and password when the user tries to access my restricted web page. Here's my complete coding 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+
10 Replies


stepterr
100+
P: 157
I've found that most of the time the syntax errors are actually caused by the line before the one it reports.

In your case I noticed that the line before has "nameif(". If that is how it actually is in your code I would start there. It looks like the word "name" should be part of the comment above.
Dec 15 '07 #2

P: 14
I've found that most of the time the syntax errors are actually caused by the line before the one it reports.

In your case I noticed that the line before has "nameif(". If that is how it actually is in your code I would start there. It looks like the word "name" should be part of the comment above.

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 i'm still getting 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 #3

stepterr
100+
P: 157
Try changing your if statement to this.

[PHP]if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="Image gallery"');
header('HTTP/1.0 401 Unauthorized');
echo 'This page requires authentication!';
exit;
}[/PHP]

As soon as I did that I was prompted with the pop up box. Let me know how that works for you.
Dec 16 '07 #4

P: 14
Try changing your if statement to this.

[PHP]if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="Image gallery"');
header('HTTP/1.0 401 Unauthorized');
echo 'This page requires authentication!';
exit;
}[/PHP]

As soon as I did that I was prompted with the pop up box. Let me know how that works for you.
Lovely it works this time. The difference between your code and mine is that you use the echo command to display the authorisation comment, whereas I place that comment within quotes....looks like these error messages sometimes are misleading?......anyway, I got my code from a book retailing in a store.....mmmm.
Looks like i'l be sticking to this site for php support...thanks again.
Now i'l work on the rest of my code...
Dec 17 '07 #5

stepterr
100+
P: 157
....looks like these error messages sometimes are misleading?......
Yeah, unfortunately sometimes that is the case, but at least it does point you in the general area that you need to start looking. Glad that worked for you!
Dec 17 '07 #6

P: 14
Yeah, unfortunately sometimes that is the case, but at least it does point you in the general area that you need to start looking. Glad that worked for you!
I've started working on the remainder of my code which involves making an sql query to validate the user's username and password.

I get the error "Parse error: syntax error, unexpected T_STRING in /home/allummfa/public_html/auth.php on line 24".

I've gone over the code carefully (which incidentally I got from a php book) and perhaps the SELECT command is not quite right. I had created another file called Vars.inc which contains the database table info and other info needed by the mysql functions. With my php book I should'nt have to be seeking help from forums but there you go. I figure once I get this project working ok the I should be ok with future projects. The vars.inc file has the code:

<?php
$host = "localhost";
$user = "my username";
$password = "mypassword";
$database = "UserAccount";

CREATE TABLE Valid_User (
user_name CHAR(25) NOT NULL,
password CHAR(25) NOT NULL,
create_date DATE NOT NULL,
PRIMARY KEY(user_name));
?>

I'm not fully sure if the Vars.inc file should be saved as an .inc file as it is now or as a .php file.
This is the main block of code which you know of already:

<?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');
echo 'This page requires authentication!';
exit;
}

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;
echo 'Couldn’t connect to server.';
$db = mysql_select_db($database,$connection)or die;
echo '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;
echo 'Couldn’t execute query.';
$num = mysql_num_rows($result);
if ($num < 1)
{

echo 'The User Name or Password you entered is not valid.';
exit;

}
}
include(“imagegallery.inc”);

?>
Dec 18 '07 #7

stepterr
100+
P: 157
I just edited the quotes a little and added a space before the word "AND". I was able to get the prompt to come up after changing it. See if this works for you also.
[PHP]
$sql = "SELECT user_name FROM Valid_User WHERE user_name = ‘$user_name’ AND password = md5(‘$user_password’)";[/PHP]
Dec 18 '07 #8

P: 14
I just edited the quotes a little and added a space before the word "AND". I was able to get the prompt to come up after changing it. See if this works for you also.
[PHP]
$sql = "SELECT user_name FROM Valid_User WHERE user_name = ‘$user_name’ AND password = md5(‘$user_password’)";[/PHP]

Hi it works now, many thanks.
I see php can be tricky. I'm going to work on the sql database now......
Dec 19 '07 #9

stepterr
100+
P: 157
You're welcome, glad it worked! I've found that making small tweeks to the lines surrounding a syntax error usually does the trick until you find out for sure which one is the problem. You might also want to try commenting out lines in order to help you narrow down the problem when it might not be straight forward, that usually helps me quite a bit when I'm stuck.
Dec 19 '07 #10

P: 14
You're welcome, glad it worked! I've found that making small tweeks to the lines surrounding a syntax error usually does the trick until you find out for sure which one is the problem. You might also want to try commenting out lines in order to help you narrow down the problem when it might not be straight forward, that usually helps me quite a bit when I'm stuck.
ok thanks for the tip.
Dec 23 '07 #11

Post your reply

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