The code below doesn't connect:
$file_open = fopen("logindetails.txt","r");
if($file_open){
$user = fgets($file_open);
$pass = fgets($file_open);
$server = fgets($file_open);
}
echo "user: ",$user,"password: ",$pass,"server: ",$server;
#$db_handle = mysql_connect($server,$user,$pass);
but outputs:
user: root password: usbw server: localhost
logindetails.txt contains:
root
usbw
localhost
while this code connects:
$user = "root";
$pass = "usbw";
$server = "localhost";
echo "user: ",$user,"password: ",$pass,"server: ",$server;
#$db_handle = mysql_connect($server,$user,$pass);
I can't get this working and I can't guess why.
The variables you read with fgets in also contain linebreaks.
$user == "root\n";
$pass == "passw\n";
Which is why MySQL won't recognize them as valid.
So, you should either:
trim() them
instead use an INI file
better yet some config.php instead of a text file.
Related
I'm using a PHP file stored on my host to connect to a database stored on the same host, this is working fine.
I am using the below to connect to the database (example connection details)
<?php
$db = new PDO('mysql:host=localhost;dbname=myDB', 'myusername', 'mypassword');
My question is; seeing as I have specified the password (and other details) to connect to my server in my PHP file, can't someone with the direct link to my PHP file just download it and open it in a text editor to see those details?
If so, should I be passing the connection details to the php file like this:
<?php
$server = $_POST['server'];
$database = $_POST['database'];
$username = $_POST['username'];
$password = $_POST['password'];
$db = new PDO('mysql:host=$server;dbname=$database', $username, $password);
Expanding a bit on my comment, ideally you want to have this in separate files, one used for global configuration you can then import to your other modules like the example below.
Config.php file:
<?php
$HOST = 'hostname';
$DB = 'dbname';
$USER = 'username';
$PWD = 'password';
... other variables and global config ...
?>
DB Connection File:
<?php
include 'config.php';
$db = new PDO("mysql:host=$HOST;dbname=$DB", $USER, $PWD);
?>
Notice how the string inside the PDO connection is double quoted, because if single quoted, string interpolation won't work.
Your variable $server and $database are not interpreted correctly as you are using single quote '. You need to use double quote " to correctly pass variable values. (Refer for more details What is the difference between single-quoted and double-quoted strings in PHP?) Change your code as below.
<?php
$server = $_POST['server'];
$database = $_POST['database'];
$username = $_POST['username'];
$password = $_POST['password'];
// Replaced ' with "
$db = new PDO("mysql:host=$server;dbname=$database", $username, $password);
I have a txt file which contains the log in credentials separated by newline. I want to pick up data from that file and set the connection string according to that. Here's the code for that.
$db = array(3);
$myFile = "sqlaccess.txt";
$handle = fopen($myFile, 'r');
if(!feof($handle))
{
for($i=0;$i<4;$i++)
{
$db[$i] = fgets($handle);
echo $db[$i]; echo "<br>";
}
}
else
{
fclose($handle);
}
$dbhost = $db[0];
$dbuser = $db[1];
$dbpass = $db[2];
$dbname = $db[3];
the echo command displays everything correctly as saved in the file.
Now the connection string is:
$conn = mysqli_connect($dbhost,$dbuser,$dbpass, $dbname);
This is not working. Connection fails
but connection is succesful if i hard code this as follows:
$conn = mysqli_connect('localhost','root','password', 'Newdb');
but hardcoding is not a good practice. so What could be going wrong in my code??
fgets will not strip \n from the returned string, so you either need to trim them yourself:
$db[$i] = trim(fgets($handle));
Or use the file function to replace your read loop:
$db = file('sqlaccess.txt');
If you choose the latter, your code is simplified to:
$myFile = 'sqlaccess.txt';
$db = file($myFile);
$dbhost = $db[0];
$dbuser = $db[1];
$dbpass = $db[2];
$dbname = $db[3];
I am using PHP nuke 7.6
In config.php i have changes the some code -
$dbhost = "localhost";
$dbuname = "root";
$dbpass = "";
$dbname = " phpnuke";
$prefix = "nuke";
$user_prefix = "nuke";
$dbtype = "MySQL";
$sitekey = "SdFk*fa28367-dm56w69.3a2fDS+e9";
$gfx_chk = 0;
$subscription_url = "";
$admin_file = "admin";
After complete the configuration i got the following message.
"There seems to be a problem with the MySQL server, sorry for the inconvenience. We should be back shortly."
Please help me to shutout this problem.
Thanks
Maybe the space before phpnuke in $dbname?
$dbname = " phpnuke";
I am stumped. I have a login.php page that contains the SQL login info (hostname, database, username, password) included at the top of my index.php page (using require_once). The login info is within a conditional statement that determines whether I am on the remote server or the local testing server and offers up the correct login info. Now this is where it gets weird, it doesn't work (as in it does not make a connection with the database) when I go directly to the index.php page on the remote server however it does work on the testing server and it works if I visit the login.php site on the remote server first (which doesn't echo anything) and then visit the index.php page on the remote server. I have caches disabled. Why is this happening and how can I fix it? try it yourself by visiting http://distantfuturejosh.com/playground/pendingaxioms first (it won't work), then visiting http://distantfuturejosh.com/playground/pendingaxioms/login.php (a blank page is served) and then returning to http://distantfuturejosh.com/playground/pendingaxioms (it will work and you'll see the images appear).
here is the login.php page:
<?php
$requesturi = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$pos = strpos($requesturi, "www.distantfuturejosh.com");
if ($pos === 0)
{
$db_hostname = "xxxxxxxx";
$db_database = "xxxxxxxx";
$db_username = "xxxxxxxx";
$db_password = "xxxxxxxx";
}
else
{
$db_hostname = "localhost";
$db_database = "xxxxxxxx";
$db_username = "root";
$db_password = "root";
}
?>
Try using
session_start();
at beginning of your file.
try using if ($pos === false) instead of if ($pos === 0) like this
<?php
$requesturi = $_SERVER['HTTP_HOST'];
$pos = strpos($requesturi, "distantfuturejosh");
if ($pos === false)
{ //local
$db_hostname = "localhost";
$db_database = "xxxxxxxx";
$db_username = "root";
$db_password = "root";
}
else
{ //remote
$db_hostname = "xxxxxxxx";
$db_database = "xxxxxxxx";
$db_username = "xxxxxxxx";
$db_password = "xxxxxxxx";
}
?>
I suppose you check user login and include login.php in your other PHP pages with include /subfolder/login.php;
I have a ton of experience connecting to MySQL, but I'm attempting to connect to a SQL Server for the first time.
I've been given an IP address, server name, and a port. I've tried every combination I can think of for the server string to make the connection, but nothing appears to be working:
"127.0.0.1:1443\servername"
"127.0.0.1,1443\servername"
"servername\127.0.0.1:1443"
"servername\127.0.0.1,1443"
etc...
All fail.
I guess I'm asking for the correct formatting of the server parameter string to connect to MS SQL from a php script.
$ip = '127.0.0.1';
$port = '1433';
$server_name = 'servername';
$server = "$ip,$port\servername"; // <-- this is the line I need help with
$username = 'user';
$password = 'pass';
$con = mssql_connect($server, $username, $password)
or die('Could not connect to the server!');
Thanks in advance!
if you are using linux
$con = mssql_connect($server_name.":".$port,$username,$password);
or on windows
$con = mssql_connect($server_name.",".$port,$username,$password);
to connect mssql server
$ip = '127.0.0.1';
$port = '1433';
$server_name = 'servername';
$server = "$ip.":".$port";
$username = 'user';
$password = 'pass';
$con = mssql_connect($server, $username, $password)
or die('Could not connect to the server!');
This should work