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

syntax error, unexpected T_STRING in /home/allummfa/public_html/Vars.inc on line 8

P: 14
Hi all,

I'm having trouble with mysql.
I've just finished my php coding for HTTP authentication and with some help am now getting a login window pop up whenever I click on a link on my website that directs to Auth.php.
The code for this 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="family pics"');
header('HTTP/1.0 401 Unauthorized');
echo 'This webpage requires athentication!';
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("familypics.inc");




?>

My problem is I now cannot get the mysql query portion of my coding to work. When I enter the username and password in the login window that pops up, I get the error "Parse error: syntax error, unexpected T_STRING in /home/allummfa/public_html/Vars.inc on line 8".

I'm actually learning php from a book but i dont have much material to learn mysql. The database is created by my web hosting company called UserAccount. The scripts I have used to create the database table and store information needed by the php mysql functions are stored in a file called Vars.inc. This file is located in my html public folder, alongside my html and php files. The contents of Vars.inc are below:

<?php

$host = "localhost";
$user = "myusername";
$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've been researching in google and tried a few changes with no joy.
Can anyone see whats wrong in the Vars.inc file? Should I really use the .inc extension?
I have entered an "include" command in my Auth.php file pointing towards Vars.inc. I assume that is ok.
Help!
Dec 19 '07 #1
Share this Question
Share on Google+
6 Replies


code green
Expert 100+
P: 1,726
Expand|Select|Wrap|Line Numbers
  1. unexpected T_STRING in /home/allummfa/public_html/Vars.inc on line 8
This is exactly what it says on the tin. A syntax error in Vars.inc on line 8.
If shown is the true contents of Vars.inc, the error occurs at CREATE TABLE. PHP does not know what this means.
You need to use the same mysql() functions and syntax used in Auth.php.
Dec 20 '07 #2

P: 14
Expand|Select|Wrap|Line Numbers
  1. unexpected T_STRING in /home/allummfa/public_html/Vars.inc on line 8
This is exactly what it says on the tin. A syntax error in Vars.inc on line 8.
If shown is the true contents of Vars.inc, the error occurs at CREATE TABLE. PHP does not know what this means.
You need to use the same mysql() functions and syntax used in Auth.php.

Ok thanks. I came across a couple sites that teaches a little mysql and i've amended my script as a lot were missing. I no longer get syntax errors.
But the snippets of info I get from the internet has left me confused. The Vars.inc code allows me to create a database table and also the lines in my code that contains the $host, $user and $password should represent my server's logins so the the php script can connect to the database. Am I right?
After which php does a query to match the user's logins with those in the database table, correct?
If this is the case where in the database table script do I store the user's logins (the table is called valid_user) so that when that user logs in via my website, he gets access to the restricted webpage? In the meantime I had tested the script by attempting to log in via the website just using random characters and I got the error generated from the Auth.php file (not from the Vars.inc file): "Could'nt connect to server". It appears the script was not even connecting successfully, let alone doing a query?
Am I missing something? The code is below. I hope you can shed some light. The Auth.php file which you have seen already is the same. Also is the .inc extension for the Vars.inc file correct for this application? I think I'm close to making all this finally work...help.



<?php

$host = "localhost";
$user = "my server's username";
$password = "my server's password";
$database = "UserAccount";

$connection = mysql_connect($host,$user,$password);
if (!$con)
{
die('Oh no I cannot connect!: ' . mysql_error());
}


/*Create table in
useraccount database*/

mysql_select_db($database, $connection);
$sql = "CREATE TABLE valid_user
(
user_name varchar(25) NOT NULL, password varchar(25) NOT NULL, PRIMARY KEY ( 'user_name' ))";

mysql_query($sql,$connection);
mysql_close($connection);
?>
Dec 22 '07 #3

code green
Expert 100+
P: 1,726
that contains the $host, $user and $password should represent my server's logins so the the php script can connect to the database. Am I right?
Yes
After which php does a query to match the user's logins with those in the database table, correct?
Your connection function connects to the mysql server.
Then you need to connect to the database.
Then you can query any table in the database.
If this is the case where in the database table script do I store the user's logins (the table is called valid_user) so that when that user logs in via my website, he gets access to the restricted webpage?
The users log in details are generally stored in a table specifically for that purpose.
So a web form is required where potential users can enrol.
After submit, this form calls a script that inserts the users details in to the table.
Your log in script querys this table checking the log in details against the data stored
In the meantime I had tested the script by attempting to log in via the website just using random characters and I got the error generated from the Auth.php file (not from the Vars.inc file): "Could'nt connect to server". It appears the script was not even connecting successfully, let alone doing a query. Am I missing something??
This is fromAuth.php. Hopefully you can see your mistake
[PHP]$user_name = trim($_SERVER[‘PHP_AUTH_USER’]);
$user_password = trim($_SERVER[‘PHP_AUTH_PW’]);
$connection = mysql_connect($host,$user,$password)or die;[/PHP]
The connection function must contain the host address,user name, password
of the MySql server particular to the database you are connecting to.
Then you need mysql_select_db('database name') to actually connect to the database.
Hope this points you in the right direction
Dec 24 '07 #4

P: 14
Yes
Your connection function connects to the mysql server.
Then you need to connect to the database.
Then you can query any table in the database.

The users log in details are generally stored in a table specifically for that purpose.
So a web form is required where potential users can enrol.
After submit, this form calls a script that inserts the users details in to the table.
Your log in script querys this table checking the log in details against the data stored

This is fromAuth.php. Hopefully you can see your mistake
[PHP]$user_name = trim($_SERVER[‘PHP_AUTH_USER’]);
$user_password = trim($_SERVER[‘PHP_AUTH_PW’]);
$connection = mysql_connect($host,$user,$password)or die;[/PHP]
The connection function must contain the host address,user name, password
of the MySql server particular to the database you are connecting to.
Then you need mysql_select_db('database name') to actually connect to the database.
Hope this points you in the right direction

Ok thanks for this.
I would like to manually code in the usernames and passwords for now, as this is for a family website so I dont really want the public registering online to see my photos. I will just code in my friends' logins. I got the php coding from a book but that book does'nt say much on mysql hence my problems.
Anyway I sort of understood this line: "$connection = mysql_connect($host,$user,$password)or die". That is why I included these lines: "
$host = "localhost";
$user = "allummfa_goodguy";
$password = "my server's password";
$database = "UserAccount";
Where allummfa_goodguy is the username I chose when the mysql database was set up by the webhosting wizard (which is different from the username I use to login to the control panel of my web hosting site). UserAccount is the actual name of the database. The password is what I chose for the mysql database.

But now I have removed these lines to hopefully simplify things and now all my coding is as 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');
echo 'Sorry, this page requires authentication!';
exit;
}

else{

$user_name = trim($_SERVER[PHP_AUTH_USER]);
$user_password = trim($_SERVER[PHP_AUTH_PW]);
$connection = mysql_connect("localhost","allummfa_goodguy","pass word for the mysql database")or die;
echo 'Couldnt connect to server.';
$db = mysql_select_db("UserAccount",$connection)or die;
echo 'Couldnt select database.';

$user_name = "papa";
$user_password = "mama";

$sql = "SELECT user_name FROM valid_user WHERE user_name = $user_name AND password = md5($user_password)";
$result = mysql_query($sql)or die;
echo 'Couldnt 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");
?>

I figure the line below should allow the script to connect to the server:
$connection = mysql_connect("localhost","allummfa_goodguy","mysq l database password")or die;

This line below should allow the script to select the database:
$db = mysql_select_db("UserAccount",$connection)or die;

These lines below should allow the script to query the table:
$sql = "SELECT user_name FROM valid_user WHERE user_name = $user_name AND password = md5($user_password)";
$result = mysql_query($sql)or die;

Note that valid_user is the name of the table.
And finally, this is the part I'm not sure of, how to manually code in my friends' username and password so that they can login on my website. I have attempted this by using these lines below in my complete coding:

$user_name = "papa";
$user_password = "mama";

Also note that the valid_user table in the database contains these fields:

user_name varchar(15)
password varchar(15)

Armed with all these scripts, things should be working but guess what, I still get the "could'nt connect to server" message.
My webhosting tech support says the server and database are ok.
I appreciate your help so far.
It looks like i'm still missing something.....
Dec 26 '07 #5

code green
Expert 100+
P: 1,726
things should be working but guess what, I still get the "could'nt connect to server" message.
Do you mean you see this
Expand|Select|Wrap|Line Numbers
  1. 'Couldnt connect to server.';
Well, this is coming from this piece of code and you will see it even if the database connection is succesful
[PHP]$connection = mysql_connect("localhost","allummfa_goodguy","pass word for the mysql database")or die;
echo 'Couldnt connect to server.';
$db = mysql_select_db("UserAccount",$connection)or die;
echo 'Couldnt select database.';[/PHP]Because you are using the echo command to output this string every time.
I think you mean [PHP]$connection = mysql_connect("localhost","allummfa_goodguy","pass word for the mysql database")
or die('Couldnt connect to server.');
$db = mysql_select_db("UserAccount",$connection)
or die('Couldnt select database.');[/PHP]
Dec 27 '07 #6

P: 14
Do you mean you see this
Expand|Select|Wrap|Line Numbers
  1. 'Couldnt connect to server.';
Well, this is coming from this piece of code and you will see it even if the database connection is succesful
[PHP]$connection = mysql_connect("localhost","allummfa_goodguy","pass word for the mysql database")or die;
echo 'Couldnt connect to server.';
$db = mysql_select_db("UserAccount",$connection)or die;
echo 'Couldnt select database.';[/PHP]Because you are using the echo command to output this string every time.
I think you mean [PHP]$connection = mysql_connect("localhost","allummfa_goodguy","pass word for the mysql database")
or die('Couldnt connect to server.');
$db = mysql_select_db("UserAccount",$connection)
or die('Couldnt select database.');[/PHP]
Yes you are right, I did miss that.
At last the authentication works, after a minor tweak of the SELECT line!
I can apply this on my website now.
Thanks for all your help.
I'l come back if any problems in the future.
Dec 29 '07 #7

Post your reply

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