How to find out which php libraries are connected. The principle of including files in PHP

I propose to talk a little about the PHP programming language, and specifically touch on the topic of the extension CURL, i.e. the ability to interact with different servers using different protocols from the PHP script itself.

Before proceeding with the consideration of curl, I want to remind you that we have already touched on the PHP language, for example, in the material about uploading to Excel in PHP or the ability to authenticate in PHP, and now let's talk about the ability to send requests to PHP.

What is CURL?

CURL is a PHP function library with which you can send requests, such as HTTP, from a PHP script. CURL supports protocols such as HTTP, HTTPS, FTP and others. You can send HTTP requests using the GET, POST, PUT methods.

CURL can be useful in cases where you need to call a remote script and get the result or just save the HTML code of the page being called, in general, everyone can find their own use, but the point is that you can send requests during the execution of the script.

Including the CURL library in PHP

In order to use the CURL library, you need to include it accordingly.

Note! We'll use PHP 5.4.39 on Windows 7 as an example, and Apache 2.2.22 as our Web server.

The first thing to do is copy the libraries ssleay32.dll, libeay32.dll, libssh2.dll they are located in the directory with PHP, in the Windows system directory, namely in C:\Windows\System32.

Then, in php.ini, connect the php_curl.dll library, i.e. uncomment the following line

Library not connected

;extension=php_curl.dll

Library connected

Extension=php_curl.dll

That's it, restart Apache, call the phpinfo () function and in case of a successful connection, you should have a curl section


If it is not there, then this means only one thing, that the library did not load, the most common reason for this was that the above DLLs were not copied to the Windows system directory.

CURL Example - Requesting a Remote Page to Display

In this example, we will simply request a remote page via the HTTP GET method and display its content on the screen.

We have a test directory in it, 2 PHP files are test_curl.php and test.php, where test_curl.php is the script where we will use curl, and test.php is a remote script that we will call. I commented the code in detail.

test_curl.php code

test.php code

Heading 1"; break; case 2: echo "<Н2>Heading 2"; break; case 3: echo "<Н3>Heading 3"; break; ) ) ?>

As a result, if you run test_curl.php, you will see the inscription “Heading 1” on the screen, you can experiment with passing id parameters ( in this case 2 or 3).

CURL example - call remote script and get result

Now let's try to call the script and get the result in order to process it later, for example, let's use the POST method. Leave the file names the same.

test_curl.php code

test.php code

And if we run test_curl.php, then 111 will be displayed on the screen, i.e. 1.11 resulting from the call to the remote script, multiplied by 100.

Now let's talk about functions and their constants.

Commonly used CURL functions and constants

  • curl_init - Initializes a session;
  • curl_close - Closes a session;
  • curl_exec - Executes a request;
  • curl_errno - Returns the error code;
  • curl_setopt - Sets a parameter for the session, for example:
    • CURLOPT_HEADER - value 1 means that headers should be returned;
    • CURLOPT_INFILESIZE - parameter for specifying the expected file size;
    • CURLOPT_VERBOSE - a value of 1 means that CURL will display detailed messages about all operations performed;
    • CURLOPT_NOPROGRESS – disable operation progress indicator, value 1;
    • CURLOPT_NOBODY - if you do not need a document, but only need headings, then set the value to 1;
    • CURLOPT_UPLOAD - for uploading a file to the server;
    • CURLOPT_POST - execute a request using the POST method;
    • CURLOPT_FTPLISTONLY - getting a list of files in the FTP server directory, value 1;
    • CURLOPT_PUT — execute a request using the PUT method, value 1;
    • CURLOPT_RETURNTRANSFER - return the result without displaying it to the browser, value 1;
    • CURLOPT_TIMEOUT - maximum execution time in seconds;
    • CURLOPT_URL - specifying the address to access;
    • CURLOPT_USERPWD - a string with a username and password in the form:;
    • CURLOPT_POSTFIELDS - data for POST request;
    • CURLOPT_REFERER - sets the value of the HTTP header "Referer: ";
    • CURLOPT_USERAGENT - sets the value of the HTTP header "User-Agent: ";
    • CURLOPT_COOKIE - contents of the "Cookie:" header that will be sent with the HTTP request;
    • CURLOPT_SSLCERT - file name with a certificate in PEM format;
    • CURLOPT_SSL_VERIFYPEER - value 0 to disable verification of the remote server certificate (default 1);
    • CURLOPT_SSLCERTPASSWD is the password for the certificate file.
  • curl_getinfo - Returns information about the operation, the second parameter can be a constant to indicate what exactly needs to be shown, for example:
    • CURLINFO_EFFECTIVE_URL - last used URL;
    • CURLINFO_HTTP_CODE - The last received HTTP code;
    • CURLINFO_FILETIME - modification date of the loaded document;
    • CURLINFO_TOTAL_TIME — operation execution time in seconds;
    • CURLINFO_NAMELOOKUP_TIME - server name resolution time in seconds;
    • CURLINFO_CONNECT_TIME - time taken to establish a connection, in seconds;
    • CURLINFO_PRETRANSFER_TIME - time elapsed from the start of the operation to the readiness for the actual data transfer, in seconds;
    • CURLINFO_STARTTRANSFER_TIME - time elapsed from the start of the operation until the transfer of the first data byte, in seconds;
    • CURLINFO_REDIRECT_TIME - time taken to redirect, in seconds;
    • CURLINFO_SIZE_UPLOAD - number of bytes when uploading;
    • CURLINFO_SIZE_DOWNLOAD - the number of bytes when downloading;
    • CURLINFO_SPEED_DOWNLOAD - average download speed;
    • CURLINFO_SPEED_UPLOAD - average upload speed;
    • CURLINFO_HEADER_SIZE - the total size of all received headers;
    • CURLINFO_REQUEST_SIZE - the total size of all sent requests;
    • CURLINFO_SSL_VERIFYRESULT The result of the SSL certificate verification requested by setting the CURLOPT_SSL_VERIFYPEER parameter;
    • CURLINFO_CONTENT_LENGTH_DOWNLOAD The size of the downloaded document, read from the Content-Length header;
    • CURLINFO_CONTENT_LENGTH_UPLOAD - size of uploaded data;
    • CURLINFO_CONTENT_TYPE The content of the Content-type header received, or NULL if this header was not received.

You can see more about CURL functions and constants for them on the official PHP website -

Pair of glasses...

PHP doesn't have built-in import framework like python, java or .net. There are several ways to use libraries in PHP.

    compile them into PHP binary. This is the most advanced way and is usually not desirable unless you have special needs.

    Install them as PHP modules on the server and enable them in PHP.ini. From a PHP programmer's point of view, these extensions are part of PHP - always available. It's just easier to add and remove without having to rebuild PHP itself.

    Install the PHP code on the server somewhere and include() it in your PHP script.

    Save a copy of the library to your project and include it in your PHP script.

Page At a basic level, the code is either part of the interpreter (static or dynamic) or it's plain old PHP code that includes() ed in your project.

For your purposes, I can only suggest that you stick with the standard PHP distribution (choose a good Linux OS and use that PHP). Then almost all the libraries you'll need at the interpreter level are available as add-on packages, and the complexity of that is left to those who do it every day.

On RedHat/Centos you can run:

yum install php php-memcached php-gd php-pecl

Page As for all the other libraries you might want to use, it's probably best to use a good PHP framework that will take care of all this for you.

Some examples:

  • Zend Framework
  • CakePHP
  • codeigniter
  • etc...

(not in any order, just the ones that come to mind)

Provided you've used the standard approach of using RPM or similar to manage the compiled ones in the PHP and extensions aspects, then a good robust framework will take care of including all of your additional PHP library code that you need.

What the end result is: You are focused on delivering the product, not on the entire infrastructure that you would otherwise have to learn and invent.

The php.ini page is parsed and run on PHP startup (each time for command line, once per server run in apache). It defines many settings, includes many modules, configures those modules, and so on.

In fact, you can override some settings in php.ini with PHP's ini_set() function. However, this only works for some settings. Others must be set prior to running the script.

When running under apache, you can add lines to .htaccess directives and which completely override PHP.ini for that directory/virtual host.

(please correct my syntax and remove this note if it's wrong)

ServerName www.example.com DocumentRoot /home/joe/site/docroot php_value include_path "/home/joe/site/php-code"

Page In response to your question #6 about your own library and the best way to package it, I suggest you first evaluate the need for a library. And if you really do something, find out the most common way people do it. If it's a simple library, then a .php file with a good website is enough.

Page Maybe a bit rambling, but I hope this points you in the right direction.

Igor Guzey

This is not an exact and final instruction for action using the correct terms and best options. It is rather a log of the current problem.

Task: Add GD2 support/module to PHP

As usual, we start training on the local computer, which, of course, works under Windows. More precisely Windows XP SP2. Next, we will connect GD2 in PHP configured through ports or manually built under FreeBSD.

Connecting GD2 to PHP on Windows

We find in the distribution:
\php-4.3.9-Win32.zip\php-4.3.9-Win32\extensions\
php_gd2.dll library, i.e. version 2.
and copy php_gd2.dll to c:\windows
in the file c:\windows\php.ini we find the line extension=php_gd2.dll and remove the comment.
Reboot Apache. Everything.

If you naively first remove the comment in php.ini and think that this is enough, then after restarting Apache, a diagnostic will appear:
"Unable to load dynamic library "./php_gd.dll" - The specified module could not be found."

Connecting GD2 to PHP under FreeBSD

PHP already installed from ports

More precisely, not only PHP, but in general everything (php, MySQL, Apache, ...) was configured using ports.

# cd /usr/ports/lang/php4-extensions # make config add a checkmark: "GD library support" # make deinstall # make reinstall # apachectl -k graceful soft restart or # apachectl restart hard restart

PHP built by hand

In this case, preliminary work is required to prepare JPEG and ZLIB

Setting JPEG Support

Possible options

JPEG from port
# cd /usr/ports/graphics/jpeg # make got directory work/jpeg-6b don't do make install
JPEG from distribution
# cd /usr/dist/ # wget ftp://ftp.uu.net/graphics/jpeg/jpegsrc.v6b.tar.gz # gunzip -c jpegsrc.v6b.tar.gz | tar xf - # cd /usr/dist/jpeg-6b # ./configure # make

Need to install zlib support

From the port
# cd /usr/ports/archivers/php4-zlib # make Libraries have been installed in: /usr/ports/archivers/php4-zlib/work/php-4.4.4/ext/zlib/modules

Building PHP

# cd /usr/dist/php-4.4.4 # ./configure --with-mysql --with-apache=/usr/dist/apache_1.3.37 --enable-memory-limit --with-gd without directory installs PHP's built-in GD library. --with-jpeg-dir=/usr/ports/graphics/jpeg/work/jpeg-6b if jpeg is from port or /usr/dist/jpeg-6b if jpeg is from distribution --with-zlib-dir=/ usr/ports/archivers/php4-zlib/work/php-4.4.4/ext/zlib/modules # make install && make clean

Testing

gif.php:png.phpjpg.php:gd_info.php:
key VAL$val) echo "
$key $val"; ?>



Passed errors

When installing the JPEG library, after "make" the "make install" command was made with the expectation that new files will go to /usr/local and then automatically be found there

# ./configure --with-mysql --with-apache=/usr/dist/apache_1.3.37 --enable-memory-limit --with-gd --with-jpeg-dir --with-zlib-dir= /usr/ports/archivers/php4-zlib/work/php-4.4.4/ext/zlib/modules

But for some reason the JPEG didn't connect. There was no negative diagnosis and no result.

Foolishly, I poked like a blind kitten trying to put it this way, that way. Either through everything built-in, then through the downloaded, then from the ports. All this disgrace was accompanied by unnecessary installations, which caused overlays due to which there were errors at the time of PHP installation:

# ./configure --with-mysql --with-apache=/usr/dist/apache_1.3.37 --enable-memory-limit --with-gd=/usr/local Thank you for using PHP. # make install /usr/dist/php-4.4.4/ext/gd/gd.c:1151: undefined reference to `gdImageRotate" this seems to indicate a directory for GD, but the diagnostics refers to the built-in directory. # ./configure - -with-mysql --with-apache=/usr/dist/apache_1.3.37 --enable-memory-limit --with-gd=/usr/local --with-jpeg-dir=/usr/local --with -png-dir=/usr/local --with-zlib-dir=/usr/ports/archivers/php4-zlib/work/php-4.4.4/ext/zlib/modules ... /usr/dist/php-4.4.4/ext/gd/gd.c:837: undefined reference to `gdImageColorMatch" /usr/dist/php-4.4.4/ext/gd/gd.c:1151 : undefined reference to `gdImageRotate" *** Error code 1

Trying to prepare all modules separately and point to them exactly.

The note: the adaptive version of the site is activated, which automatically adjusts to the small size of your browser and hides some details of the site for ease of reading. Enjoy watching!

Good day to all blog readers site on! For the last 2 months, my work for the company has been closely related to pure PHP (the use of frameworks and CMS is a taboo), I have already forgotten what Joomla is :) so it will be more appropriate to continue writing higher than ever.

Today we will talk about how files are connected in PHP. As you may have noticed, any website engine (Joomla, Wordpress, OpenCart, DLE and everything, everything, everything) consists of thousands of .php (and not only php) files and they all interact with each other, which actually forms the mechanism of work site.

Now forget about the engines and imagine another example: we have many pages on the site, each of them has the same menu, site header and footer. Naturally, we will not copy the same thing from file to file, otherwise it would have turned out to be an ancient HTML site from the 90s. At a minimum, we will break everything into 4 files:

  • index.php - the main part of the page, this file will change
  • menu.php - site menu
  • header.php - site header
  • footer.php - site footer

In addition to the main page (index.php), for example, we can also have pages with a list of products (category.php) and pages of the products themselves (product.php). It will be convenient to connect the menu.php, header.php, footer.php files to these pages. Thanks to this approach, if we add a new menu item, then we will add it only once (in menu.php), and not 3 times (in index.php, product.php, category.php).

Therefore, the question arises: "How to attach a file in PHP?". There are 2 instructions in PHP for this purpose:

And their derivatives:

I draw your attention to the fact that include and require are not functions, they are so-called language constructs, so the brackets are not needed.

What is the difference between include and require

include And require- this is exactly the same, with the only difference being that if an error occurs (for example, if the specified file does not exist and it cannot be connected), include will return an error like Warning, after which the site will continue working, and require will issue a , which will completely stop further page loading.

That is, the require instruction is more radical and rigid. It's up to you to decide which is better to use, by and large it is recommended to always choose a more strict option, that is, require, but personally I find it more pleasant to type the word include :)

include_once and require_once do the same as include (include a file), but at the same time they make sure that each file is included no more than once. They are especially useful if several people are working on a project and you need to make sure that some file is not added several times.

Attention:

include_once And require_once- work slower and consume more RAM than include and require. This is logical and is due to the fact that they have to remember all the files that were connected and each time they connect, check whether this file has already been connected.

Let me illustrate how file connection works in PHP:

When including files (any, for example: html, txt, php, xml, etc.) PHP script, their content is stupidly inserted into the place of include. In other words, include (and I also mean require, since they are the same thing) is like copying (CTRL+C) code from somewhere and pasting (CTRL+V) into a file (for example, in product. php) and then saved and run as usual: http://yoursite.net/product.php

In order to distinguish the main files, such as product.php from those that we include in it (menu.php, header.php, footer.php), the included files are renamed as follows: menu.inc.php, header.inc.php , footer.inc.php. Thanks to this, when we go into the folder, we will immediately see where the main files are and where the auxiliary ones are.

Attention:

This technique (adding inc) is used only for visual convenience and does not carry any functional differences. But visual convenience is also very important, so try to always structure and do everything in the same style.

include with return value

Remember the keyword? So in PHP there is such a wild thing as a return inside an include. I've never used it and never seen others use it, but it's possible, keep some.php example:

Include some.php:

// Output string: PHP ?>

Some more facts

  • You can include files anywhere in the PHP code, including inside ;
  • Connection (include) is triggered during the execution of the script, nothing happens in the included files beforehand;
  • The included file has the same as the line where include is declared. That is, if somewhere in the middle of the file, then the global scope, and if inside the function, then the local one. Once again, I emphasize that include is the same if you took and copied the code into a file in place of include.

Thank you all for your attention and have a great weekend!

By default, the extension for working with MySQL (php_mysql.dll library) is not connected to PHP and the corresponding functions are not available. To connect the library, you need to make changes to the php.ini configuration file and copy additional libraries to the c:/windows/system32 directory.

1. Tell PHP (Apache) where php.ini is (Apache 2.x only)

If you are using Apache version 2 and above, be sure to add the PHPIniDir directive to the Apache configuration file (httpd.conf), which allows you to specify the exact location of the php.ini file. After adding the directive, restart Apache and verify that the startup was successful.

PHPIniDir "c:/php"

Note

Note that the forward slashes / are used when writing the path, which is not typical for Windows, which uses backslashes \\. The fact is that both Apache and PHP were originally designed to work on unix systems, where forward slashes are used. In order to avoid possible hard-to-diagnose problems, we strongly recommend using forward slashes in unix format in Apache and PHP configuration files.

This assumes that the php.ini configuration file is stored in the c:/php directory. To make sure that Apache found the php.ini configuration file, run the phpinfo() function.

Printing PHP configuration information

echo phpinfo();
?>

Find the row value in the resulting purple tables "Configuration File (php.ini) Path". It should contain the path to the php.ini file used, including the file itself. If only a directory is listed on this line, it means that Apache could not find the php.ini file.

C:/php/php.ini // Correct - php.ini found and used
C:/windows/ // Wrong - php.ini not found

Note

In earlier versions of Apache, there was no PHPIniDir directive, and as a result, there were a lot of errors related to the incorrect location of the php.ini file. For Apache version 1.3, php.ini must be located in the Windows system directory, usually: c:/Windows. Using the PHPIniDir directive in Apache version 2 solves this problem radically.

After you have made sure that Apache is using the correct php.ini, you can start editing it to connect the MySQL extension.

2. Set up the extension_dir directive in PHP.INI

The extension_dir directive specifies the directory where the php extension libraries are located, including the php_mysql.dll library. If you installed php in the c:/php directory, then the extension libraries are usually located in the ext subdirectory (c:/php/ext). Check if this is the case and set the correct value of the extension_dir directive.

extension_dir = "c:/php/ext"

3. Connect the extension to work with MySQL

To do this, find the line in php.ini:

;extension=php_mysql.dll

And remove the comment symbol from it - a semicolon.

Extension=php_mysql.dll

4. Copy the additional library libmysql.dll to c:/windows

If you have PHP connected to Apache as a module, then to connect the MySQL extension, you need to copy the additional library libmysql.dll from the c:/php directory to the c:/windows/system32 directory. If php is connected to Apache as a CGI application, then no additional library needs to be copied.

5. Reload Apache

Reload Apache for all changes to take effect.

6. Check the installation of the extension

To verify that the MySQL library has successfully connected to PHP, run the phpinfo() function. Look through the "purple tables" and look for a section named MySQL. If such a section exists, it means that the extension has successfully connected.

Checking PHP interaction with MySQL

1. Check if your MySQL server is running.

Before you start checking the interaction between PHP and MySQL, make sure that the MySQL server is running for you. This can be done by opening the list of Windows services: "Start" | "Control Panel" | "Administration" | "Services". Find the service named MySQL and make sure it is in the running state (third column of the table).

The second way to make sure the MySQL server is running is to press the "Crtl" + "Alt" + "Del" buttons and in the "Processes" tab, find the process named mysqld.exe. Instead of the mysqld.exe process, the following processes can also run: mysqld-nt.exe, mysqld-max-nt.exe, mysqld-debug.exe.

2. Run test php script

$dblocation = "127.0.0.1" ;
$dbname = "test" ;
$dbuser = "root" ;
$dbpasswd = "" ;

$dbcnx = mysql_connect ($dblocation , $dbuser , $dbpasswd );
if (! $dbcnx )
{
echo "" ;
exit();
}
if (!

{
echo "" ;
exit();
}

if(! $ver )
{
echo "

Request error

" ;
exit();
}
echo
mysql_result($ver , 0 );
?>

If the test is successful, then the version number of the MySQL server will be displayed. Otherwise, descriptions of the errors that occurred will be displayed on the screen.

Errors when connecting the extension to work with MySQL

Errors like: Call to undefined function

All errors containing the phrase "Call to undefined function" point to an unconnected PHP extension. If such errors occur when working with MySQL functions, then this means that the PHP library for working with MySQL is not connected - php_mysql.dll

Error message example:

Fatal error: Call to undefined function mysql_connect()

For a solution to this problem, see the beginning of the article.

No MySQL block in phpinfo()

If everything is done correctly, but the MySQL extension does not connect and even the MySQL block is missing in the output of the phpinfo() function, then check the version of the libmysql.dll library located in c:/windows/system32.

  1. Compare the size of the libmysql.dll library located in c:/windows/system32 with the size of the library of the same name that was installed with PHP. They must be equal.
  2. Move the libmysql.dll libraries to the c:/windows directory and all subdirectories. Delete all found duplicates and leave only one required library - the one that was copied from the c:/php directory.

Note

The libmysql.dll library of the same name is also supplied with the MySQL server. However, these are different libraries, and if the MySQL library is located in the c:/windows/system32 directory, then PHP will not be able to connect the extension. The libmysql.dll library can be automatically copied to c:/windows/system32 when MySQL server is installed. The presence in the system directory of a library from an old version of PHP can also become an obstacle.

Clean page of the test script

The script shown in the listing below is often used as a script for checking the interaction between PHP and MySQL on our site. Its differences from the similar php script given at the beginning of the article are the @ symbols before calling the mysql_connect() and mysql_select_db() functions. These characters are used to suppress error output to the browser. It is useful to use them on a live site on the Internet to prevent confidential information from being displayed in the browser, but when debugging scripts, it is not necessary to disable error output, because. it can make it difficult to diagnose the problem.

PHP script that checks PHP communication with MySQL server

$dblocation = "127.0.0.1" ;
$dbname = "test" ;
$dbuser = "root" ;
$dbpasswd = "" ;

$dbcnx = @ mysql_connect ($dblocation , $dbuser , $dbpasswd );
if (! $dbcnx )
{
echo "

Sorry, mySQL server is not available

" ;
exit();
}
if ( [email protected]
mysql_select_db ($dbname , $dbcnx ))
{
echo "

Unfortunately the database is not available.

"
;
exit();
}
$ver = mysql_query("SELECT VERSION()");
if(! $ver )
{
echo "

Request error

"
;
exit();
}
echo
mysql_result($ver , 0 );
?>

If a blank page is displayed as a result of the execution of this script, then, as a rule, this means that the php_mysql.dll library is not connected to PHP. To more accurately diagnose the problem, perform the following sequence of actions.

  1. Make sure you are running other PHP scripts that do not use the MySQL database.
  2. Remove the @ symbol before calling mysql_connect and mysql_select, which is used to suppress error output to the browser. After that, more complete error information will be displayed in the browser, which will help solve the problem.

Error: Can't connect to MySQL server on "127.0.0.1" (10061)

If, when executing the test php script, an error similar to the following is generated:


Can't connect to MySQL server on "127.0.0.1" (10061)
in C:\www\panel\htdocs\test.php on line 7

This indicates that the MySQL database server is not running. A more accurate diagnosis of this problem is given above in paragraph 1 of the subsection "Checking the interaction between PHP and MySQL".

Error: Access denied for user "root"@"localhost" (using password: YES)

Presence of the following error:

Warning: mysql_connect() :
Access denied for user "root"@"localhost" (using password: YES)
in C:\www\panel\htdocs\test.php on line 7

Indicates that you are trying to connect to a MySQL database with an incorrect password. Check the value of the $dbpasswd variable in the test script. By default, the MySQL server is installed with a blank root user password. If you set a password for the root user yourself, then add it to the $dbpasswd variable of the check script.

Any questions you may have about connecting to the PHP extension to work with, you can ask on our forum dedicated to installing and configuring Apache, PHP and extension libraries.

What else to read