A
Anonymous
Guest
I have Apache, PHP, and Postgres installed on one CentOS 7.3. I have a .PHP script that does not use interactive user input that successfully connects to the Postgres database. So I know PHP and Postgres work. I now want to create new .PHP files that accept user input then use that input as credentials to log into the database.
I am trying to get the PHP page to take the credentials to then authenticate to a Postgres database. I use this PHP script (called a.php):
I use this PHP script named welcome.php (it was based on a TutorialsPoint example):
I have now posted the first PHP file and the subsequent welcome.php file. Therefore my code is all on this post that I want to get to work. The credentials work when I use this PHP script by itself.
Here is the standalone script that does not accept user input but works:
(Whenever I run the script above, I subsequently drop the table that it created.)
Ports 80 and 5432 are open on the CentOS server between the Windows computer with a web browser. I tested the port and the IP address. I created a firewall rule to block it. Then I unblocked it. I can say that the relevant ports are open between the web browser and the CentOS server that runs Apache, PHP, and Postgres.
When I open a web browser on a Windows computer and go to the PHP page, I enter the correct credentials into the PHP page. I see this error after I click submit
I am trying to get the PHP page to take the credentials to then authenticate to a Postgres database. I use this PHP script (called a.php):
Code:
<!DOCTYPE HTML>
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
<!DOCTYPE HTML>
<html>
<body>
I use this PHP script named welcome.php (it was based on a TutorialsPoint example):
Code:
echo "Favorite name is " . $_POST["name"] . ".<br>";
echo "Favorite email is " . $_POST["email"] . ".";
$host = "host=127.0.0.1";
$port = "port=5432";
$dbname = "dbname=foobar";
$credentials = "user=" . $_POST["name"] . " password=" . $_POST["email"];
echo " ";
echo $credentials; //I am certain the correct credentials are being used.
echo " ";
$db = pg_connect( "$host $port $dbname $credentials" );
if(!$db){
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
$sql =<<<EOF
CREATE TABLE COMPANY
(ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL);
EOF;
$ret = pg_query($db, $sql);
if(!$ret){
echo pg_last_error($db);
} else {
echo "Table created successfully\n";
}
pg_close($db);
echo $name
?>
I have now posted the first PHP file and the subsequent welcome.php file. Therefore my code is all on this post that I want to get to work. The credentials work when I use this PHP script by itself.
Here is the standalone script that does not accept user input but works:
Code:
<?php
$host = "host=127.0.0.1";
$port = "port=5432";
$dbname = "dbname=foobar";
$credentials = "user=jdoe password=jdoe@so.com";
$db = pg_connect( "$host $port $dbname $credentials" );
if(!$db){
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
$sql =<<<EOF
CREATE TABLE COMPANY
(ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL);
EOF;
$ret = pg_query($db, $sql);
if(!$ret){
echo pg_last_error($db);
} else {
echo "Table created successfully\n";
}
pg_close($db);
?>
(Whenever I run the script above, I subsequently drop the table that it created.)
Ports 80 and 5432 are open on the CentOS server between the Windows computer with a web browser. I tested the port and the IP address. I created a firewall rule to block it. Then I unblocked it. I can say that the relevant ports are open between the web browser and the CentOS server that runs Apache, PHP, and Postgres.
When I open a web browser on a Windows computer and go to the PHP page, I enter the correct credentials into the PHP page. I see this error after I click submit
What can I do to have the "welcome page" show a successful authentication to the Postgres database?Error : Unable to open database.