[freetds] Re: PHP & FreeTDS

Walls Rob W Contr 75 CS/SCBS Rob.Walls at HILL.af.mil
Wed Aug 25 14:29:38 EDT 2004


I never grude security advice. The /usr/share/php directory is not
accessible through the web server, but it's probably a good idea to use a
.php extension anyway. Thanks.

I changed the single quote to double (duh, right!?!), but that didn't change
the problem. I still don't know if this is an issue with the connect string,
or the DSN, or something else? I tried executing a PHP script containing
odbc_connect from the PHP command line and got the same results. It appears
that the MS SQL server is not seeing the connection attempt.
The freetds.conf, odbc.ini, and odbcinst.ini files are very similar to the
format shown below and should work, well they DO work with isql, just not
PHP. Grrr...

Is anyone specifically using RH Enterprise Linux ES, v3.0 that has gotten
ODBC to work with PHP (without paying a 3rd party for a driver)? I'd love
advice from anyone, but I wonder if that platform is somehow 'odd' and will
force me to recompile. Is there a detailed ABC checklist somewhere written
by one of you gurus with comments and test points for frustrated people like
me?

rww


-----Original Message-----
From: freetds-bounces at lists.ibiblio.org
[mailto:freetds-bounces at lists.ibiblio.org] On Behalf Of Daniel Fazekas
Sent: Tuesday, August 24, 2004 6:22 PM
To: FreeTDS Development Group
Subject: Re: [freetds] Re: PHP & FreeTDS


On Aug 25, 2004, at 1:09, Walls Rob W Contr 75 CS/SCBS wrote:

> I know this is not a PHP group, but I hoping someone knows what to do 
> to make this work with PHP without recompiling.

Note that your PHP apparently supports shared extensions, so you could add
support for the MSSQL extension without recompiling the whole thing, if that
sounds better than being stuck with ODBC.

> Warning: SQL error: [unixODBC][FreeTDS][SQL Server]Unable to connect 
> to data source, SQL state S1000 in SQLConnect in 
> /usr/share/php/common.inc on line
> 25

I hate giving unrelated and unasked for advice, but be very cautious of
using an extension such as ".inc" for holding php code. That could all too
easily lead to security problems, unless special precaution is taken to
configure Apache not to serve a ".inc" file, or to have it run through the
php interpreter. If such a file ends up in a web-accessible directory,
people could enter the file's address and since that extension does not
normally invoke the php interpreter, the full source code will be sent to
the browser instead.

Now /usr/share/php/ does not sound like a directory anyone would make
accessible through the web, so most probably there's no risk involved in
this case. Sorry if you were aware of this, I just felt it was worth
mentioning. Generally it's better to use extensions like ".inc.php"  
instead.

> I'm using this code to connect:
> $connection_string =
> 'DRIVER=TDS;SERVERNAME=db75cs-891-3;UID=$DB_LOGIN;PWD=$DB_PASSWORD;
> DATABASE=
> $DB;';
> $connection = odbc_connect( $connection_string, $DB_LOGIN, 
> $DB_PASSWORD);

Would it be unfeasible for your project to move most of the connection
parameters into the separate configuration files? I'm not really familiar
with these jumbo ODBC connection strings. ;)

However, note that since you are setting $connection_string with single
apostrophes, the $variables in the string are not getting expanded and are
kept without modification. That is, it remains UID=$DB_LOGIN, and won't get
changed to UID=whatever_is_in_$DB_LOGIN, which is probably what you wanted.
You'd have to quote the string with " for that expansion to take place.  
This is the big difference between ' and " in php. I can't guarantee this
small change will make it all work, though. :)


The following works for me:

In /usr/local/etc/freetds.conf:

[TheNameSpecifiedInFreetdsConf]
host=this.is.the.real.host.name.or.ip.of.the.server.lan
port=1433
tds version=8.0
client charset = utf-8

In /etc/odbcinst.ini:

[TDS]
Description = FreeTDS
Driver = /usr/local/lib/libtdsodbc.so

In /etc/odbc.ini:

[TheOdbcIniDataSourceName]
Driver = TDS
Servername = TheNameSpecifiedInFreetdsConf


And in the PHP code you just initialize your connection as

$conn = odbc_connect('TheOdbcIniDataSourceName', 'username', 'password');


[When using mssql_connect(), you would have to specify
'TheNameSpecifiedInFreetdsConf' as the server name.]


> PHP finds an odbc.ini file in /etc/php.d/ which loads 
> /usr/lib/php4/odbc.so

That sounds odd. PHP does not look for or use a file called odbc.ini in
order to load its shared extensions. odbc.so is probably loaded from your
php.ini instead, which is in /etc/php.ini on Red Hat by default.

--
fds

_______________________________________________
FreeTDS mailing list
FreeTDS at lists.ibiblio.org
http://lists.ibiblio.org/mailman/listinfo/freetds



More information about the FreeTDS mailing list