0
I have to build a php website which has to connect to an Ingres database server.

I have downloaded sourceforge pjbs JDBC-PHP bridge. I installed this in the project folder and tried to connect to my ingres database. I don't understand which parameters fsockopen() needs, for the databasehost (VMS server) I use the vnode but ingres uses a string as port and fsockopen wants to have an integer. I use php 7.2.3 on Windows 7 Ingres 9.2 on HP OpenVMS v.8.4

<?php

require "lib/PJBridge.php";

function get_connection()
{
    $connStr = "jdbc:ingres://vnode:II7/dbname";

    $db = new PJBridge();
    $result = $db->connect($connStr, $user, $password);

    if(!$result){
        die("Failed to connect");
}
return $result;}


<?php
class PJBridge {

    private $sock;
    private $jdbc_enc;
    private $app_enc;

    public $last_search_length = 0;

    function __construct($host="vnode", $port=-1, $jdbc_enc="ascii", $app_enc="ascii") {

        $this->sock = fsockopen($host, $port);

        $this->jdbc_enc = $jdbc_enc;
        $this->app_enc = $app_enc;
}
yolande
  • 1
  • 1

1 Answers1

0

ingres uses a string as port - cast it to an int then. you should probably also verify that it actually returns a numeric string in range (valid port ranges, for ipv4 and ipv6, is 1-65535. (port 0 also exist but isn't available on most systems))

$port = "9999";
$old = $port;
if (false === ($port = filter_var ( $port, FILTER_VALIDATE_INT, array (
        'options' => array (
                'min_range' => 1,
                'max_range' => 65535 
        ) 
) ))) {
    throw new \LogicException ( "port is not a valid integer in range 1-65535! ($old)" );
}
// here, $port is guaranteed to be an int in range 1-65535.

as for what options socket_connect wants, check this example, connecting to example.com on port 80 to download the html website on it:

$fp = fsockopen ( "www.example.com", 80, $errno, $errstr, 30 );
if (! $fp) {
    echo "error connecting: $errstr ($errno)<br />\n";
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite ( $fp, $out );
    while ( ! feof ( $fp ) ) {
        echo fgets ( $fp, 128 );
    }
    fclose ( $fp );
}

which was all taken from the fsockopen documentation.

and as a final note, i have no experience with Ingres, but raw tcp apis are becoming exceedingly rare, are you sure there's no higher level apis available for Ingres? Edit: yes there is, check http://php.net/manual/en/book.ingres.php , using that extension would almost certainly be much easier than using raw tcp sockets

hanshenrik
  • 19,904
  • 4
  • 43
  • 89
  • `echo(0x65535);` shows `415029`, it looks like writing `65535` as a binary is useless here. – A.L May 22 '18 at 13:15
  • @A.L whoa, no idea how that 0x got in there, thanks. guess i was debating whether to write 0xFFFF or 65535 , and messed up (0xFFFF makes sense because it's shorter and it's exactly the 2 bytes that the port number is made up of, it's an uint16_t port ) – hanshenrik May 22 '18 at 13:32
  • To my knowledge http://php.net/manual/en/book.ingres.php is from 2013 and is not longer supported by Action (Ingres) – yolande Jun 04 '18 at 08:49