Creating a PDO Object with Rewriting On - php

I'm having a weird situation here.
I'm trying create a PDO object, like this:
$dbh = new PDO('mysql:host='.$hostname.';dbname='.$dbname,$username, $password);
I have rewriting ON in my .htaccess file. when I try to run the script using a URL that will trigger a rewrite rule, it shows me the following error:
Fatal error: Class 'PDO' not found
I have a exception rewrite rule for a directory where the script is, named PHP, like this:
RewriteRule ^(php)($|/) - [L]
if I run the scripting directly from the directory, it runs normally with no erros.
I don't know why this is happening. any clues?
Thank you

Edit: Ok, I saw I've misinterpreted your question a bit, but I still think there is something wrong about the php.ini path. Could you check the phpinfo() output of both web calls. The basic idea of the answer keeps the same as stated below (replace CLI with the second web call ;))
OLD ANSWER
Assuming, that you have one of the more common linux distributions (ubuntu, debian, suse, etc), you might have two (or more) php.ini files. One that is pulled when using the webserver module (which seems to be an apache) and one that is used for the cli env. php supports up to one php.ini per SAPI.
In newer debian and ubuntu systems php extensions are linked in the same manner as the sites-available/enabled in the apache config.
If you have a self compiled php look for a php-cli.ini at the common location for the php.ini (which is /usr/local/share/php by default). If a php-cli.ini is present it will be used instead of the php.ini for cli commands.
(Manual Reference: http://de2.php.net/manual/en/configuration.file.php)
How to find out:
Get the phpinfo() output for the INFO_GENERAL section from you webserver. To achieve this, create a simple php file in a web-accessible directory (e.g. the DocumentRoot). Remember to delete it after you got the information you need! Please adjust the '/var/www' to something that matches your config.
$> echo '<?php phpinfo(INFO_GENERAL);' > /var/www/info.php
Now go to your browser and open http://your.host.name/info.php and look for the config property "Configuration File (php.ini) Path". Note this somewhere or just leave the page open for later reference.
On your cli this is also quite easy. The example below is a snip from my shell.
mmueller#bsd ~$ php -i | grep ini
Configuration File (php.ini) Path => /usr/local/etc/php/5.5
Loaded Configuration File => /usr/local/etc/php/5.5/php.ini
[... omitted the tail]
Compare the two paths and see if they are different.
To see why this happens, please take a look at the 'Configure Command' section. You mighty find three important configure arguments. Those three influence where php looks for it's configuration.
'--sysconfdir=/usr/local/etc/php/5.5'
'--with-config-file-path=/usr/local/etc/php/5.5'
'--with-config-file-scan-dir=/usr/local/etc/php/5.5/conf.d'
If you want to ensure the same configuration, you can do one of the following things:
Just add extension=pdo_mysql to the webserver version and see if that is enough
Remove the additional CLI config (if present), this will make php use the php.ini for all SAPIs (make a backup of the file before you do that!). Then you need to merge the rules into the php.ini that you need. (In your case there seems to be an extension=pdo_mysql missing)
Check the webserver php.ini for anything that you do not want to have in the cli version, then copy the webserver php.ini over the CLI php.ini (Do a backup first).
Delete the cli or webserver php.ini, symlink the other php.ini to the place of the deleted one. (not the best way, but I've seen that a lot on customer servers).

Solve it.
There was a empty php.ini in the public_html, I deleted it, and it worked.
thank u all

Related

PDFLib installed but not available in code or visible through phpinfo()

I think I did quite a good job on installing PDFlib on a system (Ubuntu 18.04) but something's not totally right yet. What I did so far:
Followed the instructions here: https://www.pdflib.com/fileadmin/pdflib/pdf/support/PDFlib-in-PHP-HowTo.pdf
Downloaded the correct php_pdflib.so file and placed it in the extension directory I got through phpinfo()
Added extension=php_pdflib.so in my php.ini
Ran a sudo systemctl restart apache2 to restart Apache and reload extensions
Checked with php -i | grep PDF whether the binary was loaded or not, result seems positive
PDFlib
PDFlib Support => enabled
PDFlib GmbH Binary-Version => 9.2.0
Now, when I run phpinfo(); from the web side through a file, there is no mention of PDFlib at all. When I run it through CLI, everything seems to be okay.
I also tried creating a new PDFlib() instance through CLI and web. CLI works, web doesn't.
Did I miss something in the install process?
I also tried creating a new PDFlib() instance through CLI and web. CLI works, web doesn't.
this is a typical situation. The PHP CLI and the PHP within the web server could have different configuration. So please check the extension_dir as well which the php.ini which was loaded in your web server phpinfo() output. Then you have do the the same configuration. Please check as well the PHP/Webserver log file for any error messages. Maybe it might be NTS/TS (threading) issue as well, but this will be mentioned in the error message.
Being not a good engineer today, I did several things at once, so I can't tell what exactly worked out in the end. Will write down my steps nevertheless, as it works now.
I double checked the configuration file paths and files through php -i and a phpinfo(); to see the differences between CLI and web frontend.
I removed the extension=php_pdflib from both php.ini files
I moved the php_pdflib.so from the extension directory one level up, it now lives in /usr/lib/php directly
I also renamed it to phplib.so (but that was more to break things on purpose and see what happens
I created a 30-pdflib.ini file in /etc/php/7.3/fpm/conf.d and wrote only extension=/usr/lib/php/pdflib.so in it
Added that line to /etc/php/7.3/cli/php.ini to see if there's a difference
I tried restarting Apache2 several times, but phpinfo() did not show any changes for the loaded configuration files or modules
I did a sudo reboot
Checked again and now PDFlib is loaded for CLI as well as for web
So, not sure if a hard reboot really fixed this, but it seems to me like that. Maybe this helps someone else.

Different include_path when run php script from command line?

I'm getting a fatal error on my require_once('directory/file.php'), "failed opening required...". I know the required file is in my php includes folder at c:\PHP_Includes\directory\file.php. When I look at phpinfo() I see the correct include_path: ".;c:\PHP_Includes"
But in the command line error it says include_path='.;C:\php\pear;C\Projects\project1\classes'
Any idea why the path seems to be different when running command line script?
The problem is related to the fact that WampServer has a multitude of php ini files. Make sure to keep them all in sync with the correct settings to avoid problems when one is used instead of the other.
Very useful info from http://forum.wampserver.com/read.php?2,72804:
There is actually 3 php.ini files in WampServer
This is how they are used...
C:\Wamp\bin\php\phpX.X.X\php.ini This is only used by wampserver and php cli exe. WampServer's menu is built using php and this is the
config file that is used. It is also used if you are using php through
the command line interface. You generally never need to edit this
file.
C:\Wamp\bin\php\phpX.X.X\phpforapache.ini This is a copy of the php.ini file used for your websites. When this version of php is being
used this file is copied into the apache bin folder. If you change
version of php the apache\bin\php.ini is then emptied and the
phpforapache.ini of the new version of php is loaded into whichever
version of apache is loaded.
C:\Wamp\bin\apache\apacheX.X.X\bin\php.ini This is the actual php.ini that is loaded for your websites. provided that this is the
version of apache being used. This is copied from the relevant active
php folder( phpforapache.ini).
This may seem a little confusing but it is nessecary for WampServer's
ability to change version of apache and php easily. The best thing to
remember is..
If you want to manually edit your php.ini file for websites then
always use the wampserver menu to open it . >> left click the
WampServer icon > php > php.ini
If you want to manually edit your php.ini file for command line then
open php.ini in the current php folder.

I can not find my php.ini file

While learning to set up php to be able to send mail I came across the need to edit the php.ini file. The problem is that when I go to <localhost>/~username/phpinfo.php it tells me it is located at /Library/Server/Web/Config/php however the Web directory does not exist on my server. So where is my php.ini file? I have looked at answers to the same question by others and still was not able to find it. If I need to create it, how do I go about doing that?
I am using a macbook pro as my server running Yosemite.
Thanks in advance!
UPDATE:
So it looks like I found my php.ini file but it is not where php says it is looking phpinfo.php says its looking in /Library/Server/Web/Config/php should I copy the file to this location? also my file is actually named php.ini.default, does this need to be named as just php.ini?
execute this command
locate php.ini
This will give you a list of all files with names where 'php.ini' is a part of it.
E.g.
/etc/php.ini
/etc/php.ini.rpmnew
/home/myuser/mywebsite.com/demo/local_php.ini
...
I'd rather do this:
php -i | grep ini
It will give you the info for ini configuration in the php console client. If you are executing apache or nginx you can see all the PHP settings with
<?php
echo phpinfo();

how to find php.ini in php5.3

i'm having a problem when i'm create socket program.
/* Get the port for the WWW service. */
$service_port = getservbyname('www', 'tcp');
/* Get the IP address for the target host. */
$address = gethostbyname('localhost');
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
------------------
when i'm run a program i'm get the error like this
1
Fatal error: Call to undefined function socket_create() in /opt/local/apache2/htdocs/php/TCPclient.php on line 14
i think that error because i'm not enable extension=php_sockets.dll
but when i try to locate php.ini i don't found php.ini i just found php.ini-development and php.ini-production, i change that file to enable extension=php_sockets.dll but when i'm try again to run my program i'm still get same error,
anyone know what is the problem ?
i'm give the phpinfo of my local, i'm still worried with configuration file in phpinfo that is none , are that is missing?
thanks for your answer.
It looks like you are running PHP on Mac OS. If this was installed using macports, run the following command
sudo port install php5-sockets
I see two different things here:
You think that extensions come in the .dll format. Actually that's windows. You are not running windows.
You are running a UNIXoide system A common thing is next to have a single configuration file to also have folders with more configuration files in there, so that, if scanned recursively through all configuration directories this creates a multiple file based configuration database. Yeah! Rocks! But, the problem is, you have multiple things to check in your phpinfo (if you have not noticed so far):
Configuration File (php.ini) Path - This is the main configuration file configured.
Loaded Configuration File - This is the main configuration file actually loaded.
Scan this dir for additional .ini files - PHP looks here, too.
Additional .ini files parsed - Those have been actually loaded, too.
So take a decent look at all these settings and really, really take care to not mix things. PHP configuration is documented in the PHP Manual.
Check the documentation what an extension is, how you install it etc. pp. This varies depending on your system and software management.
That is a pretty fair assumption. I am certain that because the php.ini is not found on your machine you have an installation problem.
What web server are you using? Are you installing Apache or using IIS for PHP support?
Your snapshot does not include the --enable-sockets option nor does it contain the loaded configuration file (php.ini).
Since you are running on Mac you cannot use dlla. You can compile php with socket support or try Xampp
http://www.apachefriends.org/en/xampp-macosx.html

MAMP PHP config file help

When I modify the php.ini document found in
Applications/MAMP/conf/php5/php.ini
The changes are not reflected in the php info page in the MAMP interface
WHY?
You need to restart MAMP (Or indeed any Apache Server) for changes in php.ini to take effect. Restart MAMP using the GUI.
If you are still experiencing no changes, it's possible you are editing the wrong php.ini file. In MAMP, each php version has it's own php.ini file.
To make utterly sure you are editing the right php.ini file, use the following steps:
Create a new .php file in your favourite text editor.
Type
<?php phpinfo(); ?>
and save it in MAMP's htdocs directory.
Save it as index.php
Make sure MAMP is running!
Start Safari, or any browser, and type "localhost:8888" into your
URL bar.
You should see a purple table filled with information about your
apache server.
The 6th row should be titled "Configuration File (php.ini)", and on
the right of this, a path should be displayed to the php.ini file apache is
currently using. It should look like this:
"/Applications/MAMP/bin/php/php5.4.4/conf"
Now, either use Finder to look browse to this location and edit the
.ini file with Text Edit, or use Terminal.
If you want to use Terminal, open a new window and type the
following: 'cd (and then copy and paste the path you gained from
step 6)'. It should look like something like this: 'cd
/Applications/MAMP/bin/php/php5.4.4/conf'
Now type 'ls'
Typing ls will show you the files within the 'conf' directory. The
correct php.ini file should be displayed amongst other files such as 'pear.conf'. If you can
see 'php.ini', we're nearly there. If not, you've gone wrong somewhere, so
read over the previous steps.
Now type 'sudo nano
/Applications/MAMP/bin/php/php5.4.4/conf/php.ini'
And give Terminal your password when it asks for it
The php.ini file will be opened in
Terminal's integrated text-editor, nano. Edit it, and save it using
Nano.
Simply quit terminal. 15) Restart Apache using MAMP's GUI 16)
Browse to localhost:8888 in your favourite browser. Your changes
should be reflected in the configuration screen.
I understand this instructions may be hard to follow, but just contact me if you want to use team viewer or something to get this working! Good luck
Possible answers:
You forgot to restart the Apache
webserver after the change
PHP uses a
php.ini from a different location (Check
the PHPIniDir setting in your Apache
config).
It seems that there are 2 php.ini files in the MAMP installation. The one that seems to be parsed in my case is not in the config/ folder but in the bin/php/php5.3.14/conf/php.ini

Categories