Avatar of Peter Chan
Peter Chan
Flag for Hong Kong asked on

Run the code

Hi,
How to verify that Php code below, is working?

        <form action="page.php" method="POST">
            <div class="col text-center" style="border: 0px solid blue;">
            ....

<?php
	include 'start0.php';
	$serverName = "1????,59365";

	$connectionInfo = array( "WIN-APIUFD1NJEU"=>"SS2012EXPR");
	$conn = sqlsrv_connect( $serverName, $connectionInfo);

	if( $conn ) {
	     echo "Connection established.<br />";
	}else{
	     echo "Connection could not be established.<br />";
	     die( print_r( sqlsrv_errors(), true));
	}

Open in new window

Web DevelopmentDatabasesPHPServer Software

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
Ryan Chong

To debug php, have you tried to enable the <display_errors> directive in your php.ini ?

also try to refer and configure the setting mentioned below:

error_reporting
https://www.php.net/manual/en/function.error-reporting.php
Julian Hansen

First of all put the DB connection code in its own file and included it at the top of your output page - where you have it is just in the way of the markup.

Second, test the code independently - build a test script that includes the db script that then attempts to query the db and output the result.

Once this is working you can include your db script in your output script confident that it is working.
Peter Chan

ASKER
Thanks to all.

Julian,
How to adjust it to ensure there is proper connection linked?
	error_reporting(E_ALL);
	$serverName = "1????,59365";

	$connectionInfo = array( "WIN-APIUFD1NJEU"=>"SS2012EXPR");
	$conn = sqlsrv_connect( $serverName, $connectionInfo);

	if( $conn ) {
	     echo "Connection established.<br />";
	}else{
	     echo "Connection could not be established.<br />";
	     die( print_r( sqlsrv_errors(), true));
	}

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Julian Hansen

I am not sure I understand the question?

if ($conn) will evaluate to true if connected false if not?
Peter Chan

ASKER
Yes, I echo message to that and how to capture relevant message of different status?
Julian Hansen

I still don't understand what you are asking - what does your code above not do that you want it to do?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Peter Chan

ASKER
Hi,
Sorry to that, my "echo“ line in above, is not working as expected. What to check further?
Julian Hansen

Not working how?
What are you expecting to see?
What are you actually seeing?
Peter Chan

ASKER
I want to see proper message if no connection can be created.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Julian Hansen

... and what are you seeing?

You need to help me here.

I can't see your screen I don't know what the code is doing. Based on what you have shown us it should be showing a message - if it is not it is because of something you have not shown us.

The first step is to tell us what you are seeing - what is being displayed?

How do you know the messages is not working - is it not possible it is just being hidden?

Have you tried doing this

else{
	     echo "Connection could not be established.<br />";
             file_put_contents('test_error.log', 'Connection could not be established ' . print_r(sqlsrv_errors(), true));
	     die( print_r( sqlsrv_errors(), true));
	}

Open in new window

Then check to see if the file test_error.log is created in the folder of the primary script called.
Peter Chan

ASKER
Julian,
Sorry, due to error below. Where should I put such ico file?
GET http://113.255.222.245/favicon.ico 404 (Not Found)
Julian Hansen

In the web-root of your site.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Peter Chan

ASKER
Here is page.php.

<?php
	include 'start0.php';
	error_reporting(E_ALL);
	$serverName = "1????,59365";

	$connectionInfo = array( "WIN-APIUFD1NJEU"=>"SS2012EXPR");
	$conn = sqlsrv_connect( $serverName, $connectionInfo);

	if( $conn ) {
	     echo "Connection established.<br />";
	}
	else
	{
	     echo "Connection could not be established.<br />";
	     file_put_contents('test_error.log', 'Connection could not be established ' . print_r(sqlsrv_errors(), true));
	     die( print_r( sqlsrv_errors(), true));
	}

 ?>

Open in new window

There is no other issue on page but I have got no message and test_error.log is not created.
Julian Hansen

Try this.
You need to establish if your code is running past the include so put a dump before that and after,
Note: I have changed output to debug.log and added FILE_APPEND statements from the second file_put_contents onwards
<?php
file_put_contents('debug.log', date('Y-m-d H:i:s') . ": Application start ...\n");
	include 'start0.php';
	error_reporting(E_ALL);
	$serverName = "1????,59365";
file_put_contents('debug.log',  "\tAttempting connection ...\n", FILE_APPEND);

	$connectionInfo = array( "WIN-APIUFD1NJEU"=>"SS2012EXPR");
	$conn = sqlsrv_connect( $serverName, $connectionInfo);

	if( $conn ) {
	     echo "Connection established.<br />";
file_put_contents('debug.log',  "\tConnection established...\n", FILE_APPEND);
	}
	else
	{
	     echo "Connection could not be established.<br />";
file_put_contents('debug.log',  "\tConnection Failed...\n" . print_r(slsrv_errors(), true), FILE_APPEND);
	     die( print_r( sqlsrv_errors(), true));
	}

Open in new window

Peter Chan

ASKER
Thanks a lot to Julian.
Is there a way to fire Php code on button click, instead of Post action below?

<body onload="event0()">
    <div class="container" style="background:url('./pict/ID-10028343.jpg');border: 0px solid green;background-color: C9D3DD;background-size: cover;background-repeat: no-repeat;width:1200px;height:800px;font-family:Verdana;font-size:14px;">
        <form action="page.php" method="POST">

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Julian Hansen

A script is fired when a request is made to the server (GET / POST / PUT etc).

A button click can be linked to an AJAX call that does this but ultimately you will have to be doing a request for the script to run on a user action.
Peter Chan

ASKER
Thanks. How to enable Ajax call to click event?
Julian Hansen

$('#button_id').click(function() {
   var data = { }; // any data you need to post to script
   $.post('path/to/script.php', data, function(resp) {
      // handle response if required;
   });
});

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Julian Hansen

Why do you want to call a PHP function to check if password / username is empty? Why not do it in JavaScript?
Peter Chan

ASKER
Sorry, to validate user name and password is correct vs a table remotely (with a connection).
Julian Hansen

I am still confused. You said
to show message when name and password is empty, once Login is clicked.
Why do you need a table to check for empty values?

Assuming you want to do a normal validation
<form>
  <input id="username" name="username">
  <input type="password" id="password" name="password">
</form>

Open in new window

$(funtion() {
   $('form').submit(function(e) {
       e.preventDefault();
       if (!this.username.value.length || !this.password.value.length) {
          alert('Please enter a username and password');
          return;
       }
       data = $(this).serialize();
       $.post('validate.php', data, function(resp) {
          if (resp.status) {
            window.location = resp.url
          } else {
              alert('Invalid name or password'); // Or whatever error checking you want here
          }
       }, 'json');
   });
});

Open in new window

PHP
<?php
$username = isset($_POST['username']) ? $_POST['username'] : false;
$password= isset($_POST['password']? $_POST['password'] : false;
$resp = (object) ['status' => false];
if ($username && $password) {
   $resp->status = validateUser($username, $password);
   if ($resp->status) {
     $resp->url = 'secure.php';
   }
}
function validateUser($username, $password) {
   // Validate your login here
  return true;
}
die(json_encode($resp));

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61
Peter Chan

ASKER
Sorry, how to correct it below

<html>
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
    <style>
        input {
            background-color: F0F08B;
            width: 200px;
        }

        div {
            margin: 10px auto;
        }
    </style>
    <script>
        function event0() {
            var hdr1 = "Logon Page";
            document.getElementById("hdr0").innerHTML = hdr1;
        }
        function clickCancel() {
            document.getElementById("username").value = "";
            document.getElementById("password").value = "";
            //refresh_img();
        }
$(funtion() {
   $('form').submit(function(e) {
       e.preventDefault();
       if (!this.username.value.length || !this.password.value.length) {
          alert('Please enter a username and password.');
          return;
       }
       data = $(this).serialize();
       $.post('validate.php', data, function(resp) {
          if (resp.status) {
            window.location = resp.url
          } else {
              alert('Invalid name or password is entered.'); // Or whatever error checking you want here
          }
       }, 'json');
   });
});
    </script>
</head>

Open in new window


due to such error?

(index):27 Uncaught SyntaxError: missing ) after argument list
(index):46 Uncaught ReferenceError: event0 is not defined
    at onload ((index):46)
Julian Hansen

There is no line 46 in your code.
There is no HTML in your code - the page will do nothing.
The error is saying there is no event() defined - which there is not - there IS an event0()

There is a spelling error on line 27
$(funtion() ...

Open in new window

Should be
$(function() ...

Open in new window

Peter Chan

ASKER
Hi,
How to enable click event to button below
				                    <button type="button" onclick="clickLogin()">Login</button>

Open in new window

to call this?
$(function() {
   $('form').submit(function(e) {
       e.preventDefault();
       if (!this.username.value.length || !this.password.value.length) {
          alert('Please enter a username and password.');
          return;
       }

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Julian Hansen

Just take the onclick out and make the button type="submit"

The jQuery handler works on form submit.
Peter Chan

ASKER
Can you see issue below?
(index):35 Uncaught TypeError: $.post is not a function
    at HTMLFormElement.<anonymous> ((index):35)
    at HTMLFormElement.dispatch (jquery-3.2.1.slim.min.js:3)
    at HTMLFormElement.q.handle (jquery-3.2.1.slim.min.js:3)
   
with such code?
<?php
	include 'start0.php';
file_put_contents('debug.log', date('Y-m-d H:i:s') . ": Application start ...\n");
	error_reporting(E_ALL);
$username = isset($_POST['username']) ? $_POST['username'] : false;
$password= isset($_POST['password']? $_POST['password'] : false;
$resp = (object) ['status' => false];
if ($username && $password) {
   $resp->status = validateUser($username, $password);
   if ($resp->status) {
     $resp->url = 'secure.php';
   }
}
function validateUser($username, $password) {
	$serverName = "1????,59365";
	file_put_contents('debug.log',  "\tAttempting connection ...\n", FILE_APPEND);

	$uid = "ws_login";
	$pwd = "Ws345678";
	$databaseName = "house_sch";

	$connectionInfo = array( "UID"=>$uid,
                         "PWD"=>$pwd,
                         "Database"=>$databaseName);
	$conn = sqlsrv_connect( $serverName, $connectionInfo);

die(json_encode($resp));
$tsql = "SELECT user_Name FROM user_master";

	/* Execute the query. */

	$stmt = sqlsrv_query( $conn, $tsql);

	if ( $stmt )
	{
	     echo "Statement executed.<br>\n";
	}
	else
	{
	     echo "Error in statement execution.\n";
	     die( print_r( sqlsrv_errors(), true));
	}
	if( $conn ) {
	     echo "Connection established.<br />";
file_put_contents('debug.log',  "\tConnection established...\n", FILE_APPEND);
	}
	else
	{
	     echo "Connection could not be established.<br />";
file_put_contents('debug.log',  "\tConnection Failed...\n" . print_r(slsrv_errors(), true), FILE_APPEND);
	     die( print_r( sqlsrv_errors(), true));
	}
	return true;
}

 ?>

Open in new window

Julian Hansen

That error is not referring to the code you posted.

1. Line 35 is a '{'
2. There is no $.post in that code

Usually the error means you have not included your jQuery library OR you are using jQuery before it is loaded. But I cannot tell from what you have given me.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Peter Chan

ASKER
Hi,
Can you see also this file?
<html>
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
    <style>
        input {
            background-color: F0F08B;
            width: 200px;
        }

        div {
            margin: 10px auto;
        }
    </style>
    <script>
        function event0() {
            var hdr1 = "Logon Page";
            document.getElementById("hdr0").innerHTML = hdr1;
        }
        function clickCancel() {
            document.getElementById("username").value = "";
            document.getElementById("password").value = "";
            //refresh_img();
        }
$(function() {
   $('form').submit(function(e) {
       e.preventDefault();
       if (!this.username.value.length || !this.password.value.length) {
          alert('Please enter a username and password.');
          return;
       }
       data = $(this).serialize();
       $.post('validate.php', data, function(resp) {
          if (resp.status) {
            window.location = resp.url
          } else {
              alert('Invalid name or password is entered.'); // Or whatever error checking you want here
          }
       }, 'json');
   });
});
    </script>
</head>
<body onload="event0()">
    <div class="container" style="background:url('./pict/ID-10028343.jpg');border: 0px solid green;background-color: C9D3DD;background-size: cover;background-repeat: no-repeat;width:1200px;height:800px;font-family:Verdana;font-size:14px;">
        <form action="page.php" method="POST">
            <div class="col text-center" style="border: 0px solid blue;">
                <header>
                    <h2><label id="hdr0">hdr1</label></h2>
                </header>
            </div>
            <div class="row" style="border: 0px solid red;">

                <div class="col" style="border: 0px solid blue;"></div>
                <div class="col text-right" style="border: 0px solid orange;"><b>Username:</b></div>
                <div class="col" style="border: 0px solid yellow;"><input type="text" id="username" name="username" style="background-color: white;" /></div>
                <div class="col"></div>
                <div class="w-100"></div>

                <div class="col" style="border: 0px solid blue;"></div>
                <div class="col text-right" style="border: 0px solid orange;"><b>Password:</b></div>
                <div class="col" style="border: 0px solid yellow;"><input type="password" id="password" name="password" style="background-color: white;" /></div>
                <div class="col"></div>
                <div class="w-100"></div>

                <div class="col" style="border: 0px solid blue;"></div>
				                <div class="col-md-2" style="border: 0px solid orange;"></div>
				                <div class="col-md-4" style="border: 0px solid yellow;">
				                    <button type="submit">Login</button>
				                    <button type="button" onclick="clickCancel()">Cancel</button>
				                </div>
				                <div class="col"></div>
                <div class="w-100"></div>
          </div>
      </form>
        </div>
    </body>
</html>

Open in new window

Julian Hansen

You are using jQuery slim - the ajax functions are not included
Peter Chan

ASKER
Sorry, how to include it?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Julian Hansen

Instead of
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>

Open in new window

Do
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

Open in new window

Peter Chan

ASKER
Good day Julian,
Can you help to runtime error below?

jquery-3.2.1.min.js:4 POST http://113.255.221.200/validate.php 500 (Internal Server Error)
Here is validate.php

<?php
	include 'start0.php';
file_put_contents('debug.log', date('Y-m-d H:i:s') . ": Application start ...\n");
	error_reporting(E_ALL);
$username = isset($_POST['username']) ? $_POST['username'] : false;
$password= isset($_POST['password']? $_POST['password'] : false;
$resp = (object) ['status' => false];
if ($username && $password) {
   $resp->status = validateUser($username, $password);
   if ($resp->status) {
     $resp->url = 'secure.php';
   }
}
function validateUser($username, $password) {
	$serverName = "1????,59365";
	file_put_contents('debug.log',  "\tAttempting connection ...\n", FILE_APPEND);

	$uid = "ws_login";
	$pwd = "Ws345678";
	$databaseName = "house_sch";

	$connectionInfo = array( "UID"=>$uid,
                         "PWD"=>$pwd,
                         "Database"=>$databaseName);
	$conn = sqlsrv_connect( $serverName, $connectionInfo);

die(json_encode($resp));
$tsql = "SELECT user_Name FROM user_master";

	/* Execute the query. */

	$stmt = sqlsrv_query( $conn, $tsql);

	if ( $stmt )
	{
	     echo "Statement executed.<br>\n";
	}
	else
	{
	     echo "Error in statement execution.\n";
	     die( print_r( sqlsrv_errors(), true));
	}
	if( $conn ) {
	     echo "Connection established.<br />";
file_put_contents('debug.log',  "\tConnection established...\n", FILE_APPEND);
	}
	else
	{
	     echo "Connection could not be established.<br />";
file_put_contents('debug.log',  "\tConnection Failed...\n" . print_r(slsrv_errors(), true), FILE_APPEND);
	     die( print_r( sqlsrv_errors(), true));
	}
	return true;
}

 ?>

Open in new window

Julian Hansen

500 means the server script failed. Have you checked your error logs on the server?

What do you see in debug.log (note this is different from the PHP error log - we need to see the results from both)
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Peter Chan

ASKER
Which log should we check further?
9a.pngI do not see debug.log within /var/www/html.
Julian Hansen

I am referring to the error log that PHP creates. It is not guaranteed that such a log exists - some ISP's setup PHP to create one by default others not.
This would usually exist in the root folder of your site (or the folder in which the primary script resides)

We are looking for where the PHP error is that is causing that 500.
Peter Chan

ASKER
Good day Julian,
Here is what I got

10a.png
I suspect if page.php is called by html page or not.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Julian Hansen

That is not the way to do it.

Firstly the PHP error file is not usually called debug.log so searching for that is pointless. The file is defined in your PHP.ini file.

Instead do this

create a script called pi.php
<?php
phpinfo();

Open in new window

Call this and search for the string error_log - that will tell you where the log is and what it is called.
Peter Chan

ASKER
I copy pi.php file below, to /var/www/html
<?php
phpinfo();
 ?>

Open in new window

but I am not sure if it is running properly below
12d.png
Julian Hansen

That is correct - now you need to search for "error_log"
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Peter Chan

ASKER
Sorry Julian. I go to
http://127.0.0.1/page.php

and debug.log has not been created.
Julian Hansen

We are drifting - did you find the PHP error log yet?

You have a 500 error you are trying to resolve - forget the debug log for now - until you solve the 500 that is not a priority.

Do you still have the 500 problem and if so have you found the error_log for your PHP setup?
Peter Chan

ASKER
Hi,
500 error still exists. Here are some error:

Mar 19 18:06:31 ubuntu1604 PackageKit: get-details transaction /2314_bbdacbec from uid 1001 finished with success after 1161ms
Mar 19 18:06:31 ubuntu1604 gnome-software[3350]: Failed to load snap icon: local snap has no icon
Mar 19 18:06:32 ubuntu1604 gnome-software[3350]: message repeated 4 times: [ Failed to load snap icon: local snap has no icon]
Mar 19 18:07:28 ubuntu1604 dbus-daemon[2735]: [session uid=1001 pid=2735] Activating service name='org.gnome.ControlCenter.SearchProvider' requested by ':1.17' (uid=1001 pid=2895 comm="/usr/bin/gnome-shell " label="unconfined")
Mar 19 18:07:28 ubuntu1604 dbus-daemon[2735]: [session uid=1001 pid=2735] Activating service name='org.gnome.Nautilus' requested by ':1.17' (uid=1001 pid=2895 comm="/usr/bin/gnome-shell " label="unconfined")
Mar 19 18:07:28 ubuntu1604 dbus-daemon[2735]: [session uid=1001 pid=2735] Activating service name='org.gnome.Calculator.SearchProvider' requested by ':1.17' (uid=1001 pid=2895 comm="/usr/bin/gnome-shell " label="unconfined")
Mar 19 18:07:28 ubuntu1604 dbus-daemon[2735]: [session uid=1001 pid=2735] Activating service name='org.gnome.Calendar' requested by ':1.17' (uid=1001 pid=2895 comm="/usr/bin/gnome-shell " label="unconfined")
Mar 19 18:07:28 ubuntu1604 dbus-daemon[2735]: [session uid=1001 pid=2735] Activating service name='org.gnome.seahorse.Application' requested by ':1.17' (uid=1001 pid=2895 comm="/usr/bin/gnome-shell " label="unconfined")
Mar 19 18:07:28 ubuntu1604 dbus-daemon[2735]: [session uid=1001 pid=2735] Activating via systemd: service name='org.gnome.Terminal' unit='gnome-terminal-server.service' requested by ':1.17' (uid=1001 pid=2895 comm="/usr/bin/gnome-shell " label="unconfined")
Mar 19 18:07:28 ubuntu1604 systemd[2709]: Starting GNOME Terminal Server...
Mar 19 18:07:28 ubuntu1604 dbus-daemon[2735]: [session uid=1001 pid=2735] Successfully activated service 'org.gnome.Calculator.SearchProvider'
Mar 19 18:07:28 ubuntu1604 dbus-daemon[2735]: [session uid=1001 pid=2735] Successfully activated service 'org.gnome.ControlCenter.SearchProvider'
Mar 19 18:07:28 ubuntu1604 dbus-daemon[2735]: [session uid=1001 pid=2735] Successfully activated service 'org.gnome.Nautilus'
Mar 19 18:07:29 ubuntu1604 dbus-daemon[2735]: [session uid=1001 pid=2735] Successfully activated service 'org.gnome.Calendar'
Mar 19 18:07:29 ubuntu1604 dbus-daemon[2735]: [session uid=1001 pid=2735] Successfully activated service 'org.gnome.seahorse.Application'
Mar 19 18:07:29 ubuntu1604 nautilus[3530]: g_key_file_load_from_file: assertion 'file != NULL' failed
Mar 19 18:07:29 ubuntu1604 nautilus[3530]: Could not establish a connection to Tracker: Failed to load SPARQL backend: Key file does not have group “DomainOntology”
Mar 19 18:07:30 ubuntu1604 dbus-daemon[2735]: [session uid=1001 pid=2735] Successfully activated service 'org.gnome.Terminal'
Mar 19 18:07:30 ubuntu1604 systemd[2709]: Started GNOME Terminal Server.
Mar 19 18:07:30 ubuntu1604 nautilus[3530]: g_queue_pop_head: assertion 'queue != NULL' failed
Mar 19 18:07:34 ubuntu1604 PackageKit: resolve transaction /2315_ddbcecee from uid 1001 finished with success after 2740ms
Mar 19 18:07:35 ubuntu1604 nautilus[3530]: g_queue_foreach: assertion 'queue != NULL' failed
Mar 19 18:07:35 ubuntu1604 nautilus[3530]: g_queue_free: assertion 'queue != NULL' failed
Mar 19 18:07:52 ubuntu1604 org.gnome.Shell.desktop[2895]: polkit-agent-helper-1: pam_authenticate failed: Authentication failure
Mar 19 18:08:00 ubuntu1604 dbus-daemon[3689]: [session uid=0 pid=3687] AppArmor D-Bus mediation is enabled
Mar 19 18:08:00 ubuntu1604 dbus-daemon[3689]: [session uid=0 pid=3687] Activating service name='org.freedesktop.portal.Desktop' requested by ':1.0' (uid=0 pid=3669 comm="/usr/bin/gnome-system-log " label="unconfined")
Mar 19 18:08:00 ubuntu1604 dbus-daemon[3689]: [session uid=0 pid=3687] Activating service name='org.freedesktop.portal.Documents' requested by ':1.1' (uid=0 pid=3692 comm="/usr/libexec/xdg-desktop-portal " label="unconfined")
Mar 19 18:08:00 ubuntu1604 dbus-daemon[3689]: [session uid=0 pid=3687] Activating service name='org.freedesktop.impl.portal.PermissionStore' requested by ':1.2' (uid=0 pid=3697 comm="/usr/libexec/xdg-document-portal " label="unconfined")
Mar 19 18:08:00 ubuntu1604 dbus-daemon[3689]: [session uid=0 pid=3687] Successfully activated service 'org.freedesktop.impl.portal.PermissionStore'
Mar 19 18:08:00 ubuntu1604 dbus-daemon[3689]: [session uid=0 pid=3687] Successfully activated service 'org.freedesktop.portal.Documents'
Mar 19 18:08:00 ubuntu1604 dbus-daemon[3689]: [session uid=0 pid=3687] Activating service name='org.freedesktop.impl.portal.desktop.gtk' requested by ':1.1' (uid=0 pid=3692 comm="/usr/libexec/xdg-desktop-portal " label="unconfined")
Mar 19 18:08:00 ubuntu1604 dbus-daemon[3689]: [session uid=0 pid=3687] Activating service name='org.gtk.vfs.Daemon' requested by ':1.4' (uid=0 pid=3712 comm="/usr/libexec/xdg-desktop-portal-gtk " label="unconfined")
Mar 19 18:08:00 ubuntu1604 dbus-daemon[3689]: [session uid=0 pid=3687] Successfully activated service 'org.gtk.vfs.Daemon'
Mar 19 18:08:00 ubuntu1604 dbus-daemon[3689]: [session uid=0 pid=3687] Successfully activated service 'org.freedesktop.impl.portal.desktop.gtk'
Mar 19 18:08:00 ubuntu1604 dbus-daemon[3689]: [session uid=0 pid=3687] Successfully activated service 'org.freedesktop.portal.Desktop'
Mar 19 18:08:00 ubuntu1604 gnome-system-lo[3669]: g_action_print_detailed_name: assertion 'g_action_name_is_valid (action_name)' failed
Mar 19 18:08:00 ubuntu1604 gnome-system-lo[3669]: gtk_application_set_accels_for_action: assertion 'detailed_action_name != NULL' failed
Mar 19 18:08:00 ubuntu1604 gnome-system-lo[3669]: g_action_print_detailed_name: assertion 'g_action_name_is_valid (action_name)' failed
Mar 19 18:08:00 ubuntu1604 gnome-system-lo[3669]: gtk_application_set_accels_for_action: assertion 'detailed_action_name != NULL' failed
Mar 19 18:08:00 ubuntu1604 gnome-system-lo[3669]: g_action_print_detailed_name: assertion 'g_action_name_is_valid (action_name)' failed
Mar 19 18:08:00 ubuntu1604 gnome-system-lo[3669]: gtk_application_set_accels_for_action: assertion 'detailed_action_name != NULL' failed
Mar 19 18:08:00 ubuntu1604 gnome-system-lo[3669]: g_action_print_detailed_name: assertion 'g_action_name_is_valid (action_name)' failed
Mar 19 18:08:00 ubuntu1604 gnome-system-lo[3669]: gtk_application_set_accels_for_action: assertion 'detailed_action_name != NULL' failed
Mar 19 18:08:00 ubuntu1604 gnome-system-lo[3669]: g_action_print_detailed_name: assertion 'g_action_name_is_valid (action_name)' failed
Mar 19 18:08:00 ubuntu1604 gnome-system-lo[3669]: gtk_application_set_accels_for_action: assertion 'detailed_action_name != NULL' failed
Mar 19 18:08:00 ubuntu1604 gnome-system-lo[3669]: g_action_print_detailed_name: assertion 'g_action_name_is_valid (action_name)' failed
Mar 19 18:08:00 ubuntu1604 gnome-system-lo[3669]: gtk_application_set_accels_for_action: assertion 'detailed_action_name != NULL' failed
Mar 19 18:08:00 ubuntu1604 gnome-system-lo[3669]: g_action_print_detailed_name: assertion 'g_action_name_is_valid (action_name)' failed
Mar 19 18:08:00 ubuntu1604 gnome-system-lo[3669]: gtk_application_set_accels_for_action: assertion 'detailed_action_name != NULL' failed
Mar 19 18:08:00 ubuntu1604 dbus-daemon[3689]: [session uid=0 pid=3687] Activating service name='ca.desrt.dconf' requested by ':1.0' (uid=0 pid=3669 comm="/usr/bin/gnome-system-log " label="unconfined")
Mar 19 18:08:01 ubuntu1604 dbus-daemon[3689]: [session uid=0 pid=3687] Successfully activated service 'ca.desrt.dconf'
Mar 19 18:09:01 ubuntu1604 CRON[3748]: (root) CMD (  [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi)
Mar 19 18:09:22 ubuntu1604 systemd[1]: Starting Clean php session files...
Mar 19 18:09:22 ubuntu1604 systemd[1]: Started Clean php session files.
Mar 19 18:10:02 ubuntu1604 dhclient[1828]: DHCPDISCOVER on enp0s3 to 255.255.255.255 port 67 interval 3 (xid=0x87ab9d6f)
Mar 19 18:10:05 ubuntu1604 dhclient[1828]: DHCPDISCOVER on enp0s3 to 255.255.255.255 port 67 interval 6 (xid=0x87ab9d6f)
Mar 19 18:10:09 ubuntu1604 rtkit-daemon[2046]: Supervising 3 threads of 1 processes of 1 users.
Mar 19 18:10:09 ubuntu1604 rtkit-daemon[2046]: message repeated 3 times: [ Supervising 3 threads of 1 processes of 1 users.]
Mar 19 18:10:09 ubuntu1604 rtkit-daemon[2046]: Successfully made thread 3898 of process 3829 (n/a) owned by '1001' RT at priority 10.
Mar 19 18:10:09 ubuntu1604 rtkit-daemon[2046]: Supervising 4 threads of 2 processes of 1 users.
Mar 19 18:10:10 ubuntu1604 rtkit-daemon[2046]: message repeated 2 times: [ Supervising 4 threads of 2 processes of 1 users.]
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Julian Hansen

That is not the PHP error log
Peter Chan

ASKER
Hi,
Please help to error below
[Fri Mar 20 20:53:25.037414 2020] [php7:warn] [pid 1780] [client 113.255.208.74:49893] PHP Warning:  file_put_contents(debug.log): failed to open stream: Permission denied in /var/www/html/validate.php on line 3, referer: http://113.255.248.189/
[Fri Mar 20 20:53:25.038741 2020] [php7:warn] [pid 1780] [client 113.255.208.74:49893] PHP Warning:  file_put_contents(debug.log): failed to open stream: Permission denied in /var/www/html/validate.php on line 16, referer: http://113.255.248.189/
[Fri Mar 20 20:53:25.038772 2020] [php7:error] [pid 1780] [client 113.255.208.74:49893] PHP Fatal error:  Uncaught Error: Call to undefined function sqlsrv_connect() in /var/www/html/validate.php:25\nStack trace:\n#0 /var/www/html/validate.php(9): validateUser('d', 'd')\n#1 {main}\n  thrown in /var/www/html/validate.php on line 25, referer: http://113.255.248.189/
Here is validate.php:
<?php
	include 'start0.php';
file_put_contents('debug.log', date('Y-m-d H:i:s') . ": Application start ...\n");
	error_reporting(E_ALL);
$username = isset($_POST['username']) ? $_POST['username'] : false;
$password= isset($_POST['password']) ? $_POST['password'] : false;
$resp = (object) ['status' => false];
if ($username && $password) {
   $resp->status = validateUser($username, $password);
   if ($resp->status) {
     $resp->url = 'secure.php';
   }
}
function validateUser($username, $password) {
	$serverName = "182.173.77.220,59365";
	file_put_contents('debug.log',  "\tAttempting connection ...\n", FILE_APPEND);

	$uid = "ws_login";
	$pwd = "Ws345678";
	$databaseName = "house_sch";

	$connectionInfo = array( "UID"=>$uid,
                         "PWD"=>$pwd,
                         "Database"=>$databaseName);
	$conn = sqlsrv_connect( $serverName, $connectionInfo);

die(json_encode($resp));
$tsql = "SELECT user_Name FROM user_master";

	/* Execute the query. */

	$stmt = sqlsrv_query( $conn, $tsql);

	if ( $stmt )
	{
	     echo "Statement executed.<br>\n";
	}
	else
	{
	     echo "Error in statement execution.\n";
	     die( print_r( sqlsrv_errors(), true));
	}
	if( $conn ) {
	     echo "Connection established.<br />";
file_put_contents('debug.log',  "\tConnection established...\n", FILE_APPEND);
	}
	else
	{
	     echo "Connection could not be established.<br />";
file_put_contents('debug.log',  "\tConnection Failed...\n" . print_r(slsrv_errors(), true), FILE_APPEND);
	     die( print_r( sqlsrv_errors(), true));
	}
	return true;
}

 ?>

Open in new window

Julian Hansen

The error is fairly straight forward you don't have permissions to create the debug.log file.

It is the last error that is the most relevant however

Uncaught Error: Call to undefined function sqlsrv_connect()

This is saying that the sqlsrv library is not available on your server.

The SQL library is something you have to install separately - it is not natively part of PHP. What is happening here is you are trying to use a library that has not be setup on your server.

To confirm can you run the pi.php script again
<?php
phpinfo();

Open in new window

And then search that page for sqlsrv - checking if has been installed. The error is pretty clear though - the library is not available.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Peter Chan

ASKER
you don't have permissions to create the debug.log file.

How to ensure there is enough permission granted, when running the page?

I would check other issues, per your request.
Julian Hansen

That depends on your host. If you have CPanel you can try doing it that way.

But you seem to have missed the point of my post - the permission issue is only relevant to outputing the debug.log - your real problem is you don't have the sqlsrv library on your machine.
Peter Chan

ASKER
Hi,
I did the setup, per steps below

https://medium.com/@nahomt/using-sql-server-from-your-php-apps-on-ubuntu-16-04-c85671249c45

but now sqlsrv.so and pdo_sqlsrv.so are not inside /usr/lib/php/20170718, below.

20c.png
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.