Why are "use" statements not working in my PHP scripts? - php

I'm coming back to PHP after many years and am running into a bit of an odd problem. I'm running PHP 7.1.7 on Windows and am trying to install and utilize the maclof/kubernetes-client library via Composer. I have the following simple code snippet to test things out:
<?php
require __DIR__ . '/vendor/autoload.php';
use Maclof\Kubernetes\Client;
$path = realpath(__DIR__ . '/kubeconfig.yaml');
// this works (when fully qualified)
$config = Maclof\Kubernetes\Client::parseKubeconfigFile($path);
// but this doesn't
$client = new Client($config);
?>
As you can see, when I access the static method parseKubeconfigFile using the fully-qualified class name (Maclof\Kubernetes\Client), this works. But when I try to just use the shorter name of Client, it doesn't work. Even class_exists("Client") returns false. It's as though PHP is outright ignoring the use statement, and I don't know why.
What am I missing? Is there some weird php.ini directive that governs use statements? Or is there some wacky bug in this specific version of PHP? I'm pulling my hair out right now trying to understand why this doesn't work.

Related

Security Html and PhP

can some one tell me what this following code does and what is the problem here in terms of security?
< ?php require_once '../include/' . $_GET['file']?>
i think that this _Get['file'] gets an argument and includes into the library i guess? im not sure, and also not sure about the problem in this code.
This allows the client to make the webserver execute almost any PHP script on the server. They can even execute files outside the ../include directory by including their own ../ sequences in the parameter, e.g.
yourscript.php?file=../../../somescript.php
This will then execute
require_once("../include/../../../somescript.php");
which is equivalent to
require_once("../../somescript.php");

PHP version Check

So I'm basically trying to create a tool that used across platforms, including sometimes legacy php version.
I don't plan on supporting anything less than 5.4 so I'd like to use something like below; however, instead of the application dying, I get various syntax errors. One of the first to start alerting is using brackets to define arrays.
Is there anyway to get around this?
if (version_compare(phpversion(), '5.4', '<')) {
die('This tool does not support anything < PHP 5.4<br>Your PHP version is: '.phpversion() );
}
$array= ['a','b','c'];
The file you are doing a version_check for should simply not use any newer PHP features or include any files that do. If you want the version_check to work on PHP 4, it can only use PHP 4 features.

Accessing Flickr API in CLI

I am using phpfickr and need to run it in CLI.
But when executing $ php getToken.php, but I'm not being able to getting authenticated. I have the $app_id and $secret.
Please, I am new to this and haven't found a correct solution.
The phpFlickr library you've linked to is very old.
Its most recent commit was on July 7, 2014, roughly 2.5 years ago.
It uses PHP 4 style constructors (named after the class), which have been deprecated in PHP 7 and will be removed in a future version of PHP.
It uses var keywords, another PHP 4 anachronism, which were briefly deprecated but then brought back as a semi-synonym of public.
If you really want to use this library it should be as simple as
<?php
require_once __DIR__ . '/phpflickr/phpFlickr.php';
// Make sure to fill in your API key and secret!
$flickr = new phpFlickr('your-api-key-goes-here', 'your-api-secret-goes-here');
The getToken.php file that you referenced does this. Perhaps you forgot to fill in your API key and secret?
Once you have your $flickr object you can use it to interact with Flickr's API. For example, you can do something like this to see titles of recently-posted public photos:
foreach ($flickr->photos_getRecent()['photos']['photo'] as $photo) {
echo $photo['title'] . "\n";
}
However, there are more modern options. rezzza/flickr, for example, is available on Packagist and has over 16K installs. It uses modern PHP features like namespaces, __construct() constructors, and visibility keywords. It also seems to have a more sane API, though that's subject to opinion.
If you are already using Composer you should be able to composer require rezzza/flickr, then proceed as its README suggests. If you're not using Composer, start. It is an important part of the modern PHP ecosystem.
Based on issue https://github.com/dan-coulter/phpflickr/issues/48 , adding
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
got me going.
This issue appears to be merged into master branch, but it does not exist.

Assigning the return value of new by reference is deprecated in

I'm new to PHP and Im playing around with some code to see what i can learn/do. I have a simple little project where I am connecting to a mysql db and displaying the records of a table.
When I put all my code in one file, everything runs smooth, as soon as I try to put a chunk of code in a separte PHP file and use an include or require_once I get the error Assigning the return value of new by reference is deprecated in...PEAR\config.php yadda yadda
I've done some research and I understand that passing an object by reference (&obj) is no longer acceptable in PHP 5, that's fine, but no where in my code do I have an ampersand, and again, it all works fine if it's all in the same .php file. So i figure i might be doing something wrong with my includes or something.
This is the file that 'does the work' that i am calling from my main page with a require_once
include '/config.php';
// Open the database connection
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
//Define and run the query
$query = "SELECT * FROM wp_posts";
$result = mysql_query($query);
echo mysql_error();
//Close the db connection
mysql_close();
echo "<table><tr><td>Post Title</td><td>Post Status</td><td>Post Date</td></tr>";
//Loop through result set and display data
while($tdata=mysql_fetch_array($result))
{
echo "<tr><td>$tdata[post_title]</td><td> $tdata[post_status]</td><td> $tdata[post_date]</td></tr>"; // name class and mark will be printed with one line break
}
echo "</table>";
Anyways, I know my code isn't beautiful, Im still trying to figure it all out. Thanks for any help you can provide.
Additional Information:
Ok, I know I'm a complete noob on this include stuff, and i apologize. Cant quite wrap my head around it all yet. But here's some additional info. Here is the full text of the error message I'm receving (NOTE: My code still runs and my table DOES still display)
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
Deprecated: Assigning the return value of new by reference is deprecated in C:\xampp\php\PEAR\Config\Container.php on line 111
Again, I'm using netBeans as my IDE, so in my Netbeans i went to Tools-Options and went to PHP and set C:\xampp\php\PEAR as the Global include path (Whatever the heck that means, but nonetheless it seemed like a good idea).
I guess if my include path is set right (/xampp/php...) then why am I still receiving the error. Grrr. Thanks for all your help guys.
It's the third party library you're using that's causing the problem, it's out of date and is using such things as returning by reference which are no longer considered good practice. You should upgrade the library in question. From what you've written I'm guessing it's this library. http://pear.php.net/package/Config/redirected
Try running pear upgrade config from the shell.
Your include path might be set in the wrong order.
The script seems to prefer the PEAR include directory over the current working directory.
There are several ways to fix this:
1. use absolute includes
For instance, in your case:
include dirname(__FILE__) . 'config.php'; // pre-PHP 5.3
include __DIR__ . 'config.php'; // PHP 5.3
2. change the include path
To read more about this, see this page in the php.net documentation
Open your config.php and go to line 89 and remove the & after =
from:
"$this->container =& new Config_Container('section', 'root');"
to:
"$this->container = new Config_Container('section', 'root');"
Same goes to line 166.
it is simply pear package error which describes the problem about the assignment. so we can normally replace the assignment of
=& with =
in the given line of the error statement.that will solve our problem.
I had similar problem.The solution is change name of your file. config.php gets a file in pear folder not your config file.
Just run C:\xampp\php\pear-update.bat and restart the Apache server (On Windows macchine).
For linux macchine follow the instuctions of GordonM.

PHP variables maintaining an URL string

I'm not a PHP developer, but I'm currently hacking on an internal tool so my team can take advantage of its goodness. There's an index file that looks like so:
require( ($loader_path = "../../loaderapi/") . "loader.php" );
Used like this, $loader_path will retain its value within the loader.php file.
However, we want to access this API from our team's server like so:
require( ($loader_path = "http://remoteservername/loaderapi/") . "loader.php" );
In this case the $loader_path variable doesn't retain its value. I'm guessing it has something to do with it being a full blown URL, but I might be wrong. Any idea on how I can make this work, or why I can't do it this way?
If your accessing a PHP script over HTTP, only the output of that script is returned. So your script will try to interpret the output of that remote PHP script and not its source.
If there is a connection over the file system, you may want to try file://remoteservername/loaderapi/loader.php instead.
NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO!
Remote file inclusion is a BAD idea, probably one of the biggest security flaws you can open up. Even for an internal tool this is not acceptable even if only purely for contributing bad habits.
PHP by default disables this behavior, and there is a broad push to have the ability to perform an include on a URL completely stripped from PHP (as there is no compelling reason to have this ability).
If you want to load shared resources, go through a shared file system drive (as in, don't use http, ftp, anything but file://) or better yet distribute copies of loader.php through a version control system. Loading from a single file resource opens you up to problems in the future of say a new dev overwriting loader.php and breaking everyone else's code.
There shouldn't be any real difference between the two; what you're doing is defining $loader_path, concatenating the loader.php, and passing that to require.
HOWEVER: you're defining the variable within the scope of a require, which will halt processing of the script of require fails.
Try replacing 'require' with 'include' and see if it retains the variable.
Also, note that if you are running your PHP server on a windows machine, and the php version is less than 4.3.0, neither 'require' nor 'include' can handle remote files : http://us.php.net/manual/en/function.include.php
Also, as noted before, if the .php lives on a remote server that parses php, you will not get code, but the result of the remote server processing the code. You'll either have to serve it up as a .txt file, or write php that, when processed, outputs valid php.
Have you tried splitting it into two lines:
$loader_path = "http://remoteservername/loaderapi/";
require( $loader_path . "loader.php" );
It's easier to read this way as well.
Simplify the code reading by simply putting everything on 3 lines:
$loader_path = "http://remoteservername/loaderapi/";
$page = "loader.php";
require($loader_path . $page );
Much clearer and it works.
why not just put it above the require statement? would make it easier to read too.
<?php
$loader_path = "../../folderName/"
require($loader_path . "filename")
?>

Categories