473,385 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
6 2601
code green
1,726 Expert 1GB
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
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
1,726 Expert 1GB
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
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
1,726 Expert 1GB
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
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

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

Similar topics

6
by: Ben Allen | last post by:
Hi, Im currently getting the error: Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in...
4
kestrel
by: kestrel | last post by:
I have some html code that is supposed to be displayed by php echo. But for some reason i keep getting a syntax error, and i cant figure out what is going on. Heres what i have <?php...
0
by: badkitty63 | last post by:
I am getting this error Help!!!!!! Parse error: syntax error, unexpected T_STRING in /home/desertr3/public_html/includes/languages/english/shipping.php on line 18 I am trying to add shipping...
36
by: rhys | last post by:
My Gurus and Angels -- Please pardon this old-school programmer, only recently enlightened to open-source, having been trapped in the convenience of proprietary lingos for way too long. My...
5
by: goodguyjam | last post by:
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 *...
10
by: goodguyjam | last post by:
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...
15
by: micky125 | last post by:
Lo all, Ive been getting the error message Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/moneill/public_html/input.html...
10
by: benicio | last post by:
Parse error: syntax error, unexpected T_STRING, expecting '(' in C:\wamp\www\study_group\includes\functions.php on line 19 I got this error and this syntax is from 8 to 19th line. <?php ...
14
riverdale1567
by: riverdale1567 | last post by:
Hi I am a newbie trying to get some of my first code working, yada yada yada. I have a drop down box which chooses a state then takes the post data to 'processform2.php' to use that to pull up...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.