So I have taken a example from php.net to see php ssh2 workings
so the code is
<?php
$connection = ssh2_connect('shell.example.com', 22);
if (ssh2_auth_password($connection, 'username', 'secret')) {
echo "Authentication Successful!\n";
} else {
die('Authentication Failed...');
}
?>
When I am putting wrong information it is giving me some error instead of telling me Authentication Failed.
The Error is
Warning: ssh2_connect(): php_network_getaddresses: getaddrinfo failed: Name not known in
/var/zpanel/hostdata/zadmin/public_html/whothehellcareswhatisthis/ssh2.php on line 2 Warning: ssh2_connect(): Unable to connect to
shell.example.com on port 22 in /var/zpanel/hostdata/zadmin/public_html/whothehellcareswhatisthis/login/ssh2.php on
line 2 Warning: ssh2_connect(): Unable to connect to shell.example.com in
/var/zpanel/hostdata/zadmin/public_html/whothehellcareswhatisthis/login/ssh2.php on line 2 Warning:
ssh2_auth_password() expects parameter 1 to be resource, boolean given in /var/zpanel/hostdata/zadmin/public_html/whothehellcareswhatisthis/login/ssh2.php
on line 4 Authentication Failed...
But when I am putting correct information it is not giving any error.
The problem is that shell.example.com doesn't actually exist and the call to ssh2_connect() fails to return a resource.
Your code should be checking to see if ssh2_connect() successfully makes a connection and returns a resource before attempting to use the resource with ssh2_auth_password().
$connection = ssh2_connect('some.valid.ssh.host', 22);
if (!$connection) {
throw new Exception("Could not connect to server.");
}
if (!ssh2_auth_password($connection, 'name', 'password')) {
throw new Exception("Authentication failed!");
}
// SSH connection authenticated and ready to be used.
Related
I want to send message using XMPP and I have used this code:
include 'XMPP.php';
#Use XMPPHP_Log::LEVEL_VERBOSE to get more logging for error reports
#If this doesn't work, are you running 64-bit PHP with < 5.2.6?
$conn = new XMPPHP_XMPP('hostname',5222, 'panelusername', 'panelpassword', 'xmpphp', 'gmail.com', $printlog=false, $loglevel=XMPPHP_Log::LEVEL_INFO);
try {
$conn->connect();
$conn->processUntil('session_start');
$conn->presence();
$conn->message('senderusername', 'This is a test message!');
$conn->disconnect();
} catch(XMPPHP_Exception $e) {
die($e->getMessage());
}
But it gives me this error:
Warning: require_once(XMPPHP/XMLStream.php): failed to open stream: No such file or directory in /opt/lampp/htdocs/xmppmessage/examples/XMPP.php on line 30
Fatal error: require_once(): Failed opening required 'XMPPHP/XMLStream.php' (include_path='.:/opt/lampp/lib/php') in /opt/lampp/htdocs/xmppmessage/examples/XMPP.php on line 30
In the XMPP.php file on line 30, change it from:
require_once 'XMPPHP/XMLStream.php';
to
require_once 'XMLStream.php';
$url=$row1['content_url'];
$file_handle = fopen($url,"r");
$line=fread($file_handle,400);
$line1 = wordwrap($line,400,"</br>" );
fclose($file_handle);
the above give is my code for reading data in the text file...
it is working fine in my localhost...
But it is not working in serevr.....
Warning: fopen() [function.fopen]: php_network_getaddresses:
getaddrinfo failed: Name or service not known in
/home/content/08/11968108/html/dev/index.php on line 57
Warning: fread() expects parameter 1 to be resource, boolean given in
/home/content/08/11968108/html/dev/gopi/index.php on line 58
Warning: fclose() expects parameter 1 to be resource, boolean given in
/home/content/08/11968108/html/dev/gopi/index.php on line 60
Make sure the URL in $url contains a wrapper (like http).
Explanation
fopen("www.domain.com","r");
This will look for a local file named www.domain.com
fopen("http://www.domain.com","r");
This will try to get the external file http://www.domain.com (if allow_url_fopen is enabled)
But your error could also indicate an old cached DNS record. If you can, try to change your name server on the production system (e.g.: 8.8.8.8 for the name server from google).
If you just need to handle the warnings that are thrown (as I did), you may just do this to turn them into Exceptions that can be handled:
set_error_handler(
function ($err_severity, $err_msg, $err_file, $err_line, array $err_context) {
// do not throw an exception if the #-operator is used (suppress)
if (error_reporting() === 0) return false;
throw new ErrorException( $err_msg, 0, $err_severity, $err_file, $err_line );
},
E_WARNING
);
try {
$fp = fopen("http://example.com","r");
} catch (Exception $e) {
echo $e->getMessage();
}
restore_error_handler();
Solution based on this thread/question.
I am trying to connect to my NearlyFreeSpeech MySQL database. I can login through PHPMyAdmin but not through PDO. I am using this code
$dbconn = new PDO('mysql:host=127.0.0.1;dbname='.$config['db'].'; port=3307', $config['user'], $config['pass']);
Where $config is defined in a separate file. The error I get is:
Warning: PDO::__construct() [pdo.--construct]: [2002] Connection refused (trying to connect via tcp://127.0.0.1:3307)
Error: SQLSTATE[HY000] [2002] Connection refused
and then eventually
Fatal error: Call to a member function query() on a non-object in...
If I use
mysql:host=localhost
The error I get is
Error: SQLSTATE[HY000] [2002] No such file or directory
Now I assume "Connection refused" is better than "No such file or directory", but I don't know where to go from here. Any idea why this is happening? Thank you for your help.
Try my existing functions and constant variables, you may also change those constant to an array variable you have.
define("SERVER_SQL_VERSION","mysql");
define("SQL_SERVER","localhost");
define("SQL_PORT","3306");
define("SQL_USERNAME","root");
define("SQL_PASSWORD","");
define("SQL_DB_NAME","db");
if(!function_exists('pdoConnect')) {
function pdoConnect() {
$pdo = new PDO(SERVER_SQL_VERSION.":host=".SQL_SERVER.";dbname=".SQL_DB_NAME."", "".SQL_USERNAME."", "".SQL_PASSWORD."");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
return $pdo;
}
}
There might be an issue about your concatenation, this must be working...
I also seperated the SERVER_SQL_VERSION, and added a functions to check if the driver is available...I am using the XAMPP software and only mysql and sqlite is active, if you/others try to use postgresql and so on...that must be working as well.
if(!function_exists('check_sql_version')) {
function check_sql_version() {
$sql_available = false; //make it false yet
foreach(PDO::getAvailableDrivers() as $key => $val) {
if(SERVER_SQL_VERSION == $val)
{
$sql_available = true;
}
}
//check now if sql_available is true or false
if($sql_available == true)
return true;
else
return false;
}
}
So a sample should be considered:
if(!check_sql_version()) {
echo '('.SERVER_SQL_VERSION.') is not available, you only have this drivers:<br/>';
foreach(PDO::getAvailableDrivers() as $key => $val) {
$key = $key + 1;
echo $key.') '.$val.'<br/>';
}
exit(); //exit and dont proceed
}
$stmt = pdoConnect()->prepare("SELECT * FROM accounts");
$stmt->execute();
I hope it helps!
class restRendering
{
public $RestUrl='http://www.myweather2.com/developer/forecast.ashx?uac=AnwWoM6K2.&output=xml&query=10591';
public $XSLTPath="receipe.xsl";
//setter method for test url
public function setRestUrl( $value )
{
$this->RestUrl = $value;
}
public function setXSLTPath( $value )
{
$this->XSLTPath = $value;
}
//It renders the iframe with base url and path.
public function render(){
//load the XML
$xml_Doc = new DOMDocument();
if($xml_Doc->load($this->RestUrl))
{
//load the XSL
$xsl= new DOMDocument();
$xsl->load($this->XSLTPath) or die("can not load XSLT file");
$xslt = new XSLTProcessor($xsl);
$xslt->importStyleSheet($xsl);
print $xslt->transformToXML( $xml_Doc ) or die("Trasform Error");
}
else{
echo "Can not load the url";
}
} //End of Render method.
}
While executing I am getting this error:
Warning: DOMDocument::load(): php_network_getaddresses: getaddrinfo failed: No such host is known. in D:\xampp\htdocs\sweet\restRendering.php on line 25
Warning: DOMDocument::load(http://www.myweather2.com/developer/forecast.ashx?uac=AnwWoM6K2.&output=xml&query=10591): failed to open stream: php_network_getaddresses: getaddrinfo failed: No such host is known. in D:\xampp\htdocs\sweet\restRendering.php on line 25
Warning: DOMDocument::load(): I/O warning : failed to load external entity "http://www.myweather2.com/developer/forecast.ashx?uac=AnwWoM6K2.&output=xml&query=10591" in D:\xampp\htdocs\sweet\restRendering.php on line 25
According to this error:
Warning: DOMDocument::load(): php_network_getaddresses: getaddrinfo failed: No such host is known. in D:\xampp\htdocs\sweet\restRendering.php on line 25
your server has a problem with DNS. It can't connect to www.myweather2.com because it can't translate it to an IP address.
Try
nslookup www.myweather2.com
and see what the results are. When you solve that problem, your script will probably work.
This examples are from lynda php beyond basic
config.php
<?php
defined('DB_SERVER') ? null : define("DB_SERVER", "localhost");
defined('DB_USER') ? null : define("DB_USER", "root");
defined('DB_PASS') ? null : define("DB_PASS", "");
defined('DB_NAME') ? null : define("DB_NAME", "photo_gallery");
?>
database.php
<?php
require_once('config.php');
class MySQLDatabase {
private $connection;
function __construct(){
$this->open_connection();
}
public function open_connection(){
$this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
if (!$this->connection) {
die("Database connection failed: " . mysql_error());
} else {
$db_select = mysql_select_db(DB_NAME, $this->connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
}
}
public function close_connection(){
if(isset($this->connection)) {
mysql_close($this->connection);
unset($connection);
}
}
public function query($sql) {
$result = mysql_query($sql, $this->connection);
$this->confirm_query($result);
return $result;
}
private function confirm_query($result){
if (!$result) {
die("Database query failed: " . mysql_error());
}
}
}
$database = new MySQLDatabase();
?>
index.php
<?php
require_once("../includes/database.php");
if (isset($database)) {
echo "true";
} else {
echo "false";
}
?>
Now here my problem when I tried index.php run it on the browser i get an error
error says:
Deprecated: Assigning the return value of new by reference is deprecated in C:\xampp\php\PEAR\Config.php on line 80
Deprecated: Assigning the return value of new by reference is deprecated in C:\xampp\php\PEAR\Config.php on line 166
Notice: Use of undefined constant DB_SERVER - assumed 'DB_SERVER' in C:\xampp\htdocs\photo_gallery\includes\database.php on line 13
Notice: Use of undefined constant DB_USER - assumed 'DB_USER' in C:\xampp\htdocs\photo_gallery\includes\database.php on line 13
Notice: Use of undefined constant DB_PASS - assumed 'DB_PASS' in C:\xampp\htdocs\photo_gallery\includes\database.php on line 13
Warning: mysql_connect() [function.mysql-connect]: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\photo_gallery\includes\database.php on line 13
Warning: mysql_connect() [function.mysql-connect]: [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. (trying to connect via tcp://DB_SERVER:3306) in C:\xampp\htdocs\photo_gallery\includes\database.php on line 13
Warning: mysql_connect() [function.mysql-connect]: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\photo_gallery\includes\database.php on line 13
Database connection failed: php_network_getaddresses: getaddrinfo failed: No such host is known.
I somehow fix the error by renaming config.php
So here's my question why i got this error? is config.php filename the problem?
The video tutorial from Lynda php beyond basics didn't get this error.
There is some sort of error with your local configuration, maybe because you have some third party PEAR installed, hence the error message:
Deprecated: Assigning the return value of new by reference is deprecated in C:\xampp\php\PEAR\Config.php on line 166
You should revise what packages and libraries your system use, because this error message is a sign of outdated code (Deprecated). Personally, I use XAMPP on Windows with my project also having config.php in its htdocs folder without any sorts of error like these. The problem is that the linked file is for php4. On my system, the 166th line reads as follows:
$this->container = new Config_Container('section', 'root');
config.php is a conventional and general name of calling the configuration file, there is no problem with it. Keeping it with this name lowers no security barriers which could be fixed by merely renaming it.
The other errors will be fixed if your project succeeds in loading the configuration file. Also:
Warning:
Please, don't use mysql_* functions to write new code. They are no longer maintained and the community has begun the deprecation process. See the red box?
Instead, you should learn about prepared statements and use either PDO or MySQLi. This article should give some details about deciding which API to use. For PDO, here is a good tutorial.
It might be due to presence of "config.php" and "Config.php" files in same directory.
In windows,file names are not case sensitive.
In linux, they are.