How to use TOTP / HOTP library in PHP - php

I tried to implement TOTP PHP library as another authentication for my login form. I downloaded and followed installation instruction from github as folowing code:
<?php
include('src/Factory.php');
include('src/HOTP.php');
include('src/HOTPInterface.php');
include('src/OTP.php');
include('src/OTPInterface.php');
include('src/ParameterTrait.php');
include('src/TOTP.php');
include('src/TOTPInterface.php');
use OTPHP\TOTP;
$otp = TOTP::create();
echo 'The current OTP is: '.$otp->now();
?>
Yet, I ended up with error message Warning: Unsupported declare 'strict_types' in C:\wamp64\www\otp\src\HOTP.php on line 3 and Parse error: syntax error, unexpected '?', expecting variable (T_VARIABLE) in C:\wamp64\www\otp\src\HOTP.php on line 28.
I could not figure out why it was that. Very much appreciate for your help. Thanks.

Install lower version of package like ~8.0, which works with PHP 5.5
https://github.com/Spomky-Labs/otphp/blob/v8.3.2/composer.json#L19
add this in your composer.json
"require": {
"spomky-labs/otphp": "~8.3"
}
Or use this link to download it as ZIP and add it to your project manually
https://github.com/Spomky-Labs/otphp/archive/v8.3.2.zip
Better is to use composer, because you have auto autoloader. :) That way there is no need to manually require files.

try the multiOTP class, which is PHP 5.x compatible
https://github.com/multiOTP/multiotp

Related

Codeigniter 3.1.7 valid_email issue

I have two sites using Codeigniter. Today I have updated sites to Codeigniter 3.1.7 (from 3.1.6). After that at one of this sites email validation had broken, valid_email validation rule doesn't work any more. On second site all fine. I had check out error logs and found error:
ERROR - 2018-02-06 10:13:21 --> Severity: Warning --> idn_to_ascii() expects parameter 3 to be long, string given /public_html/system/libraries/Form_validation.php 1235
Codeigniter's changlog says:
Updated Form Validation Library rule valid_email to use INTL_IDNA_VARIANT_UTS46 for non-ASCII domain names.
It seems to me that INTL_IDNA_VARIANT_UTS46 constant is not defined. Site using PHP Version 5.6.30
How can I fix this issue?
I found discussion of this problem here.
Issue is outdated ICU library (ICU version 4.2.1 was installed)
Go to system/library/email.php and replace valid_email()
public function valid_email($email)
{
if (function_exists('idn_to_ascii') && defined('INTL_IDNA_VARIANT_UTS46') && $atpos = strpos($email, '#'))
{
$email = self::substr($email, 0, ++$atpos).idn_to_ascii(self::substr($email, $atpos), 0, INTL_IDNA_VARIANT_UTS46);
}
return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
}
just get the latest library for email
Get the latest codeigniter zip file
then go to system->libraries->Email file
copy that email file and paste it into your previous Email.php library file then it will work
I think there's a better way to sort out that issue.
As we all know that email library not supported in php 7.2 and that's why error occured
idn_to_ascii(): INTL_IDNA_VARIANT_2003 is deprecated
Error Screenshot of codeigniter email library
Quick Solution :
--> Go to cPanel
--> Select PHP version
--> change it to 7.1 instead of 7.2
For my case. I am also facing this issue.
Error:
Severity: 8192
Message: idn_to_ascii(): INTL_IDNA_VARIANT_2003 is deprecated Filename: libraries/Form_validation.php
Line Number: 1234
I downloaded the Latest CodeIgniter version. Then from the system/libraries folder copied Email.php and Form_validation.php. Then replaced with Existing Email.php and Form_validation.php at the same path.
After replace, It is start working Fine..!!
Thanks #Evgeny for Guide.
The above issue would occur when you have incompatible CI and PHP version combinations. I mean newer PHP version and older CI version, vice-versa. I had PHP 7.3 on my machine and the CI repo/project I downloaded from github had CI version 3.1.5. I had to replace the system folder (of 3.1.5 with that of newer CI versions, 3.1.10 in my case) to resolve this issue

yii2 project running errors

I am installed yii2 advanced template using composer all are working fine.
System have php7.1.5. Whenever i copy advanced folder in another system it throw syntax error in TestCase.php like below
Parse error: syntax error, unexpected '?' in \advanced\vendor\phpunit\phpunit\src\Framework\TestCase.php on line 822
line 822 is $configurationFilePath = $GLOBALS['__PHPUNIT_CONFIGURATION_FILE'] ?? '';
That another system have php5.6.8
So, i am directly install yii2 advanced in that system and it is working fine.
I am check TestCase.php the file have lot of differences and the particular line also changed like below
$configurationFilePath = (isset($GLOBALS['__PHPUNIT_CONFIGURATION_FILE']) ? $GLOBALS['__PHPUNIT_CONFIGURATION_FILE'] : '');
So i want to know what is going here.My question is not clear please let me know.Thanks in advance
PHP operator ?? is available since PHP7, so u can't expect, that code from PHP7 will work on server with PHP5.
PHP 7 - Null coalescing operator

Fatal error: Class 'PHPExcel_Shared_String' not found

I have used PHPExcel for my codeigniter app and it is working perfectly in localhost, but when I host this to server, I am getting following error :
Fatal error: Class 'PHPExcel_Shared_String' not found in \xx\xx\xx
third_party\PHPExcel\Autoloader.php on line 36
There was a change introduced to the autoloader in the latest version of PHPExcel that appears to have broken backward compatibility with versions of PHP < 5.3.0
If you edit the Classes/PHPExcel/Autoloader.php file and change line 58, which should read
return spl_autoload_register(array('PHPExcel_Autoloader', 'Load'), true, true);
to
return spl_autoload_register(array('PHPExcel_Autoloader', 'Load'));
I've already made a change to the develop branch on github to test for the PHP version and execute the appropriate line
While this was not deliberate, please note that we really are trying to get users to upgrade to at least version 5.3.0 of PHP, because we can't address any of the memory/performance issues that users working with large spreadsheets complain about until we can use some of the new features available in more recent versions of PHP. Version 5.2 of PHP is no longer supported, and even version 5.3 is end-of-life and will be unsupported before the end of this year
struggled with this issue for a long time under Linux and PHP 5.4x. In the end, in addition to the fix above, I resorted to changing the code on line 73 of the Autoloader file which sets the $pClassFilePath variable from relative (using PHPEXCEL_ROOT) to absolute following the machines file tree. This might only be a hack, but it saved my sanity after several days of trying. Hope this helps someone. Cheers
I had this issue too and i solved it by changing permissions on "Shared" directory to 655.
I Hope it helps
If your server is on Linux, it can be permission problem... Just add all permissions for PHPExcel Folder in you Vendor (on server side) and all subfolders for it. I have same problem and i have solved it by this way...
What worked for me was changing PHPExcel/Autoloader.php line 81 from
if ((file_exists($pClassFilePath) === FALSE) || (is_readable($pClassFilePath) === FALSE)) {
to
if ((stream_resolve_include_path($pClassFilePath) === FALSE)) {
I prefer this approach because it didn't require me to modify file permissions and it should work in PHP 5.3.2 and later.

Laravel 4 syntax error out-of-the-box

I just installed Laravel 4 (Illuminate) and as I opened the index.php file in a browser, I was met with this error:
Parse error: syntax error, unexpected 'yield' (T_YIELD), expecting identifier (T_STRING) in /www/Laravel4/vendor/illuminate/view/src/Illuminate/View/Environment.php on line 339
I have fixed the permissions for the meta folder, and installed all the dependencies through Composer. I am running PHP version 5.5.0alpha2 on OSX 10.8.2.
That's because yield became a language construct in PHP 5.5 (used in Generators) - but someone decided that it's a good idea to use this short word to name a function:
public function yield($section)
{
return isset($this->sections[$section]) ? $this->sections[$section] : '';
}
Downgrade to PHP 5.4 (after all, it's the current mainstream version, 5.5 is not even in beta yet) and it should work fine.

PHP Not Recognizing http_get_request_headers()?

I'm calling http_get_request_headers() in a PHP file on a server running PHP 5. However, I'm getting Fatal error: Call to undefined function http_get_request_headers(). Does anyone know what the problem might be? Does this function not come with plain PHP?
No it does not. You need a PECL module for that function to work. But you can use the contents of the $_SERVER variable as stated in this comment on the php site. Alternatively you can use the apache function if this is your web server.
If you're using version >= 2 of pecl_http you'll need to use the namespace syntax for calling the functions. Check out the version 2 documentation here and example here
Basically \http\Env::getRequestHeader()
That function is part of the PECL extension.
Follow the instructions on this page to install it: http://ar.php.net/manual/en/http.install.php
Have you read the Installation and Configuration guide for the HTTP classes/functions?

Categories