I have XAMPP running on an MS Server 2012 R2. I need to make a connection to an MSSQL server which accepts only integrated win authentication.
If I simply try the below, I get a login failure and an error log on the SQL Server that SQL authentication is not an option. It only accepts connection from a certain user. Obviously the PHP script is not being run under that account.
$server = "sqlServerName";
$SQLUser = "username";
$SQLPass = "pw";
$SQLDatabase = "db";
$link = mssql_connect($server,$SQLUser,$SQLPass);
As I'm using PHP 5.3.1 sqlsrv_connect is not an option. I tried to load the php drivers for it but it's just not working. I can't change the authentication for the sql server and I can't use any other version of PHP.
I also can't turn the secure_connection on as I have to be able to connect to other sql servers which requires "normal" sql authentication:
mssql.secure_connection = Off
How to connect to my problematic sql server?
UPDATE: Upgraded xampp to the latest version. PHP is now version 5.6.8 I still can't use sqlsrv_connect() even though I installed the necessary driver and added every single dll to the php.ini. Restarted apache several times. Any clue?
error msg: Fatal error: Call to undefined function sqlsrv_connect()
Ok. It's hard to debug a server issue without being on the server but I've done a lot with php and SQL Server so I will do my best to share my experiences.
First, Very glad you updated from 5.3.1 that version of php is ancient and very insecure. Here are some sanity checks for your system. This may do nothing for you but all of it is worth checking.
First make sure you can connect to sql server using SQL Server Management studio with the credentials you provided. This means the same credentials you use in php not the windows authentication credentials. You should be able to have both connections at the same time so you can make changes and test the connection at the same time.
Enable tcp. sql server configuration manager -> SQL Server network configuration -> protocols for sqlexpress -> tcp/ip (right click)-> properties -> Enabled (yes) -> ip Addresses -> IPAll -> TCP Port 1433 -> ok
Enable sql server auth. Select server (right click) -> properties -> security -> sql server and windows authentication mode -> ok
Open sql server port on firewall. Windows Control panel -> system and security -> windows firewall -> advanced settings -> Inbound rules -> New rule -> Port -> tcp -> 1433 (or whatever) -> Allow connection -> next -> Name -> sql server -> finish -> restart computer.
Of course if you want to connect through a non-default user you need to add the user: sql server -> security -> logins (right click) -> add login -> server roles -> sysadmin -> ok
If you make any of these changes restart sql server: Sql server configuration manager -> sql server services -> sql Server (right click) -> restart.
Once you confirm you can connect with management studio here are the php configuration checks:
You can see if the extension itself is available by creating a php page with only the function phpinfo() in it. Then search for pdo_sqlsrv. If it is present the rest of these checks are probably not necessary but since you've been working this so long probably check them anyway.
sql_srv extension for php should be version 3.2 for php 5.6 you can obtain that library here
Version 3.2 requires an os extension available here Check the other requirements on the previous link. Your os may use a different extension from the one linked here.
Find your php extensions directory. this is usually {php-install-directory}/ext. Make sure you copy the appropriate version of the downloaded sqlsrv libraries into this directory. Mine are called "php_sqlsrv_55_ts.dll" and "php_dpo_sqlsrv_55_ts.dll" Yours will have 56 instead of 55 I think and the "ts" should match your php install. "ts" means thread safe, the other option is "nts" not thread safe. The one you use is dependent on your php install.
My php.ini file contains these lines extension=php_sqlsrv_55_ts.dll and extension=php_pdo_sqlsrv_55_ts.dll in that order. but I don't think order matters. and again yours will be 56 and the "ts" may be "nts".
If you made any changes based on these make sure to restart apache then check if pdo_sqlsrv is in your phpinfo() report. Also after restarting apache check the apache and php error log to see if you get specific errors about php trying to load the extensions. Post those here if you need help with them.
Once you are connected to sql server through the auth creditionals in management studio and see pdo_sqlsrv in your phpinfo() here are the last things to look into in your code.
Your code above is still for mssql extension. You probably just didn't update it with your latest changes. For sql server extension your code should look like this:
$connectionInfo = array(
'UID' => $dbuser,
'PWD' => $dbpass,
'LoginTimeout' => 1,
);
$host = $host . ', ' . $port;
$connection = sqlsrv_connect($host, $connectionInfo);
$error_messages = sqlsrv_errors();
For windows authentication exclude the uid and pwd.
$connectionInfo = array();
$conn = sqlsrv_connect( $host, $connectionInfo);
If you have more issues please tell me which step is not working so we can dig into more detail with that step.
Go back to your PHP 5.3.1 stack where mssqlconnect() was working. SQL Server integrated mode requires an authenticated user which has access to the machine running SQL Server. So if you can connect in SSMS with your user, you need to start your XAMPP,Apache/httpd with the credentials of your user. If apache is running as a service, go the services panel (services.msc on run), then go to Apache->properties->logon->'This Account' and put in your username and password so that apache service runs with your credentials, which is authenticated to connect to SQL Server instance. By default httpd is run with system account which is obviously not authenticated by SQL Server at remote machine, causing you the headache.
Related
I am trying to learn PostgreSQL and so installed it and phppgAdmin on my laptop.
I went to localhost/phppgadmin, logged in and I get all these errors on the front page:
I tried going troubleshooting on Google, but unfortunately there are not enough answers to this question.
Also, config.inc.php file looks like this after I altered it according to the Tutorial I followed to connect the Database to PHP:
$conf['servers'][0]['desc'] = 'PostgreSQL';
// Hostname or IP address for server. Use '' for UNIX domain socket.
// use 'localhost' for TCP/IP connection on this computer
$conf['servers'][0]['host'] = 'localhost';
// Database port on server (5432 is the PostgreSQL default)
$conf['servers'][0]['port'] = 5432;
// Database SSL mode
// Possible options: disable, allow, prefer, require
// To require SSL on older servers use option: legacy
// To ignore the SSL mode, use option: unspecified
$conf['servers'][0]['sslmode'] = 'allow';
// Change the default database only if you cannot connect to template1.
// For a PostgreSQL 8.1+ server, you can set this to 'postgres'.
$conf['servers'][0]['defaultdb'] = 'template1';
// Specify the path to the database dump utilities for this server.
// You can set these to '' if no dumper is available.
$conf['servers'][0]['pg_dump_path'] = '/usr/bin/pg_dump';
$conf['servers'][0]['pg_dumpall_path'] = 'C:\\xampp\\pgSql\\11.2\\pg_dumpall.exe';
Thank you in advance
EDIT: Is it possible to just user the original pgadmin?!
This error is because the version of phpPgAdmin you are running does not support php7. You can either downgrade your PHP version or run the code from git master, available at https://github.com/phppgadmin/phppgadmin.
I'm trying to browse a customer's Microsoft SQL server database with PHP but port 1433 is closed. Digging around I found out MSSQL can run in Dynamic Port Allocation mode, that means it will choose a random listen port at first execution, and will likely remain the same accross startup. I know I can find out the current port, but since likely is not always and I'd like to avoid searching for it again, is there any way to remotely discover the port to connect to?
From what I could understand by my searches this job is usually accomplished by SQLBrowser(.exe ?), but how to do this on Linux?
Update on the solution
While #Chris' answer was correct I was missing a simple but essential bit: on every change of odbc.ini you need to run:
odbcinst -i -s -f /etc/odbc.ini
to update system's DSN.
After that I could connect using
isql -v DSN_NAME username password
Troubleshooting
To check server instance:
tsql -H HOSTNAME_OR_IP -L
this will print server information, including instance names and port to which you should be able to connect using standard telnet or mssql client.
Given that your answer was correct, I had to do minor changes to make it work. I decided to write them here. Steps are basically the same. On Ubuntu/Debian:
apt-get install php5-sybase unixodbc tdsodbc
Edit /etc/odbcinst.ini and add driver details
[TDS]
Description = FreeTDS Driver
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
Edit /etc/odbc.ini and enter connection details
[SQLSRV01]
Description = SQL Server test
Driver = TDS
Trace = No
Server = SERVER_IP\INSTANCE_NAME
TDS_Version = 9.0
#Database = DataBaseName
#ReadOnly = Yes
The last two parameters are optional. Driver must match what we wrote in odbcinst.ini. The Server directive must be in that syntax (of course SERVER_IP can be an hostname too).
According to UnixODBC the next step should not be necessary, but this is what made my installation work. Run the following command (every time odbc.ini is changed)
odbcinst -i -s -f /etc/odbc.ini
After this you should be able to connect using:
isql -v SQLSRV01 nome_utente password
Or via PHP:
$db = new PDO("dblib:host=SQLSRV01;dbname=DBNAME","USERNAME","PASSWORD");
Short answer:
ODBC drivers know to contact SQL server on port 1434 to find which dynamic port is associated with a named instance. user SERVERNAME\INSTANCENAME to connect.
Long answer:
I started here which led here and here.
Eventually I found this:
If you are using mssql with multiple instances and dynamic port
allocation you can use the following:
[SQLServer2008]
Description = Production Server
Driver = TDS
Trace = No
Server = servername\instance_name
TDS_Version = 8.0
Which seems to be echoed in a similar IBM Doc:
Question
SQLServer is setup to dynamically assign ports. In the .odbc.ini file,
the Address parameter is usually set to hostname colon port number
(Address=HostName:1433), but the port may change. How should we handle
this?
Answer
For the Address parameter value, instead of entering the hostname
colon port, enter the hostname a backslash and the server instance
name.
For example, in Unix/Linux, use the IBM SQLServer Wire Protocol driver
and enter the following in the .odbc.ini file in the DSN definition
for the connection to the SQLServer data source:
Address=HostName\Server_Instance_Name
For Windows, use the ODBC Data Sources Administrator to configure a
System DSN for the data source using the IBM SQLServer Wire Protocol
driver.
Note: The parameter is Server
I am trying to connect using PHP to an SQL Server on another machine. I have found two ways of doing this. Either with odbc_connect or sqlsrvr connect.
$connection = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$serverName;Database=$db;", 'user', 'pass');
or
$conn = sqlsrv_connect($serverName, array('UID' => '', 'PWD' => ''));
The connection works if I try to connect to an SQL account. Unfortunately I cannot manage to connect using the Windows Authentication.
So far I have tried the following : used a working pass, added Trusted_Connection=yes; or Integrated Security=SSPI; . I have also tried combining this with fastcgi.impersonate = 1 or 0.
When I use directly my user and pass I get a login denied error and when I try the windows authentication with sspi I receive the following error:Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.
I've googled the problem a lot but I could not find a solution. Some people however were talking about php not using the Network Service account and that that might be the problem.
Does anyone know how I could fix this or maybe give me a lead?
Thank you in advance.
I was able to establish a trusted connection to a remote SQL server from PHP running on a seperate IIS server using:
$connectionInfo = array('Database' => $dbname);
$con = sqlsrv_connect($servername,$connectionInfo);
In php.ini:
fastcgi.impersonate = 0
mssql.secure_connection = On
In IIS for my PHP site:
Anonymous Authentication = Disabled
Windows Authentication = Enabled
Application Pool settings for site: Integrated, Network Service
Database Security on SQL Server for $dbname database:
create login: domainname\iisservername$
map domainname\iisservername$ to db_datareader privilege for $dbname database
Note: The $ in the iisservername is important. With the above settings IIS will establish a trusted connection with the SQL server without the need to store userid and password information in your PHP file. Locally the permissions used by the application pool are "Network Service". Remotely, these permissions pass through the network as user domainname\iisservername$.
Open the IIS Console
Configure your App Pool identity with the desired user which must have enough permissions in your SQL Server+Database
Select your "Specific Website"
Open "Authentication" settings
Right-click on "Anonymous Authentication" and click on "Edit"
By last select the option: Application pool identity (radio button) and press the "OK" button to apply the changes.
basically the problem is that the identity that is used to connect to the server will always be the identity of the process in which PHP is running
This link may help you.
For PHP on IIS 7.5 to connect without a username and password using SSPI authentication. I had to do the following:
In my IIS application pool advanced settings, I set it to use a custom account Domain\username to the user I needed PHP to connect as.
In my IIS specific website, I changed the Basic settings to Connect As that same Domain\username.
In my PHP code I could then connect using $conn = sqlsrv_connect($dbhost, array("Database" => $dbname, "UID" => "", "PWD" => ""));
Other notes: fastcgi.impersonate = 1
And I only have: Anonymous Authentication Enabled
*
Have you tried $conn = sqlsrv_connect($serverName);
The array is optional. Windows authentication is used as default. See this MSDN article
IIS should have Windows authentication enabled and anonymous access turned off.
Open the IIS Console
Go to your site
Open "Authentication" settings
Right-click on "Anonymous Authentication" and click on "Edit"
By last select the option: Application pool identity (radio button) and press the "OK" button to apply the changes.
Go to "Application Pools" and get the application pool name.
Open SSMS (SQL management studio), enter the relevant database and choose "Security".
Add a new user, fill only the name and save. The name of the user should be:
IIS APPPOOL\{YOUR_APP_POOL_NAME}
Good luck!
I am having issues connecting to a mssql server that is located on a network machine. I need to connect remotely through another domain example (abc.com) not on the network to access data. This domain runs mysql if that makes any difference. I am trying to access the mssql server through this php script:
<?php
$server= 'ip address:port';
$user='user';
$password= 'pass';
$con = mssql_connect($server,$user,$password);
if (!$con)
{
die('Could not connect:' . mssql_get_last_message() );
}
else{ echo 'connected';
}
I run this script through the shell on abc.com and I get:
mssql_connect(): Unable to connect to server:
I have gone through several tutorials to enabling tcp/ip access through the sql server config manager as well as allowing the specific port through the firewall.
What are some other things I should try or steps I am missing here.
Also: the ip address I am using is the one I found in the sql server config manager-> protocols for SQLEXPRESS->tcp/ip_>ip addresses->IP2->ip adress it is the correct ip address? Where can I find it if not? I am not using the localhost 127.0.0.1
In short to successfully execute the cmd.
config:
(2 instances of SQL // SQL2005 + 2008 Express instance on the remote machine, which refuses to install Management Studio 2008.
1. Enable TCP/IP Protocol
2. Enable Named Piptes
3. started sqlcmd Utility with -s \SQLEXPRESS to get the right sever-instance
C:\Program Files\Microsoft SQL Server\100\Tools\Binn
4. Go to SMSS and local sql instance properties -> Connections -> check "Allow remote connections to this server" and run this script.
EXEC sys.sp_configure N'remote access', N'1'
GO
RECONFIGURE WITH OVERRIDE
GO
Here the link for the info from Msdn:
http://msdn.microsoft.com/en-us/library/ms162773.aspx
http://msdn.microsoft.com/en-us/library/ms162816.aspx
Thanx
We are using MSSQL server 2008 on Win server 2008 R2 and have installed Apache (2.2.19) and PHP (5.2.17) separately. We have installd the MSSQL module for PHP and usingthe following connection string:
$myc = mssql_connect(Server, SiteDatabaseUsername, SiteDatabasePassword) or die('Can\'t connect to mssql Database Server: '.mssql_get_last_message($myc));
$db = mssql_select_db(SiteDatabaseName, $myc) or die('Can\'t find database: '.mssql_get_last_message($myc));
But it gives us this error:
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: localhost\MSSQLSERVER in D:\Apache2.2\htdocs\adminarea\_core.php on line 89
Can't connect to mssql Database Server:
We have used the IP:PORT, localhost\MSSQLSERVER and COMPUTERNAME\MSSQLSERVER but don't seem to be getting anywhere, can anyone help please?
A few things come to mind:
1) Make sure you have the sqlsrv driver package from MS, and make sure you're loading it properly in php.ini (use phpinfo() to verify).
2) If this was a vanilla installation of SQL Server, its likely that only windows authentication is turned on. Open the SQL Enterprise Manager and make sure you have SQL Server Authentication enabled on your database.
3) Make sure that you have enabled network access to SQL Server. I'm not immediately certain whether the new MS drivers use named pipes to communicate with the server, or if they expect that the server will be accessed via the network.
These are the issues I routinely encounter when getting PHP speaking to a new MSSQL server.