Hiển thị các bài đăng có nhãn PHP. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn PHP. Hiển thị tất cả bài đăng

Thứ Sáu, 9 tháng 8, 2024

Joomla5- php 8.1 error Attempted to load class "DOMDocument" - SOLVED


Detail: PHP 7.3 SUCCESS, PHP 8.1 + 8.2 error "DOMDocument"
Error: php 8.1 Attempted to load class "DOMDocument" from the global namespace.Did you forget a "use" statement?

Solution: CPANEL goto PHP Selector > extension check XML package checked all

 


Thứ Bảy, 29 tháng 6, 2024

Issue joomla Content-Security-Policy blocked a JavaScript .htaccess SOLVED

Browser error: Content-Security-Policy: The page’s settings blocked a JavaScript eval (script-src) from being executed because it violates the following directive: “script-src 'self' 'unsafe-inline' ...

Reason: host setting or .htaccess block scripts

Solutions: solution 1 or 2
Solution 1: .htaccess check example line

#Header set Content-Security-Policy "script-src 'self' 'unsafe-inline' https://cdn.ckeditor.com https://ajax.googleapis.com/ https://cdn.jsdelivr.net https://js.calltrk.com/ https://birdeye.com/ https://cdn.calltrk.com/ https://www.google-analytics.com/ https://widgets-v7.birdeye.com/ https://www.googletagmanager.com/ https://portal.setmysite.com/; object-src 'none';"

Remove or disable this line


Solution 2: .htaccess admin line s
<IfModule mod_headers.c>
Header always set Content-Security-Policy "script-src 'none'"
</IfModule>

 

 



Thứ Năm, 27 tháng 6, 2024

.htaccess rewrite rule change '/' to '?'

Details: .htaccess rewrite rule change '/' to '?'
http://mydomain.com/myservices/support
to
http://mydomain.com/myservices?support
 

Solution:
<IfModule mod_rewrite.c>
    RewriteRule ^myservices/(.*)$ /myservices?$1 [R=301,NC,L]
</IfModule>

Thứ Ba, 26 tháng 11, 2019

PHP - Get Country Code by live IP

Detail: function in region area or coutry
Solution: PHP get live IP from browser, get from http://www.geoplugin.net/json.gp?ip=1.2.3.4

<?php
// PHP code to obtain country, city,
// continent, etc using IP Address
 
//$ip = '52.25.109.230';
$ip= $_SERVER [ "REMOTE_ADDR" ];
 
// Use JSON encoded string and converts
// it into a PHP variable
$ipdat = @json_decode(file_get_contents(
    "http://www.geoplugin.net/json.gp?ip=" . $ip));
//print_r($ipdat);
echo 'Your IP: ' . $ip . "<br/>";
echo 'Country Name: ' . $ipdat->geoplugin_countryName . "<br/>";
echo 'Country code: ' . $ipdat->geoplugin_countryCode . "<br/>";
echo 'City Name: ' . $ipdat->geoplugin_city . "<br/>";
echo 'Continent Name: ' . $ipdat->geoplugin_continentName . "<br/>";
echo 'Latitude: ' . $ipdat->geoplugin_latitude . "<br/>";
echo 'Longitude: ' . $ipdat->geoplugin_longitude . "<br/>";
echo 'Currency Symbol: ' . $ipdat->geoplugin_currencySymbol . "<br/>";
echo 'Currency Code: ' . $ipdat->geoplugin_currencyCode . "<br/>";
echo 'Timezone: ' . $ipdat->geoplugin_timezone;
?>

Thứ Hai, 25 tháng 11, 2019

PHP string Serialize Corrector SOLVED

Reason: many string Serialize NOT right format
Solution: check and correction  string. Check many times on internet
Source : from internet. Not remember exactly. So thanks for all

 /*Serialize Corrector*/
 function serialize_corrector($serialized_string){       
        if ( @unserialize($serialized_string) !== true &&  preg_match('/^[aOs]:/', $serialized_string) ) {
             $serialized_string = preg_replace_callback( '/s\:(\d+)\:\"(.*?)\";/s',    function($matches){return 's:'.strlen($matches[2]).':"'.$matches[2].'";'; },   $serialized_string );
        }
        return $serialized_string;
    }

Thứ Ba, 3 tháng 7, 2018

PHP random array unique multi SOLVED

/*Original Array*/
$arrayIDs = ['a','aa','bb','cc'];
/*Result*/
$randomArray = [];
/*number unique*/
$numberRandom = 4;
if(count($arrayIDs)> $numberRandom){
while(count($randomArray) < $numberRandom) {
  $randomKey = mt_rand(0, count($arrayIDs)-1);
  $randomArray[$randomKey] = $arrayIDs[$randomKey];
}
}else{
$randomArray = $arrayIDs;
}

Thứ Năm, 3 tháng 8, 2017

PHP HTML special decode textarea

Description: load data have htmlspecialchars, example: html char, new line, special char.... This need decode 1 again.
Solution: decode again htmlspecialchars_decode at PHP load.
More: At prevent sql rejection $_POST have special char at Textarea. This need encode to safe data.

<php? 
echo htmlspecialchars_decode($product['COLUMN_DATA']);
?>

Chủ Nhật, 4 tháng 6, 2017

Php the best way prevent SQL Injection

Description: hacker add query sql to input _POST, _GET param. So need clean each input value.
Solution: Filter all  _POST, _GET param. 2 case:
#Case 1: Number, String with htmlspecialchars.
#Case 2: _Post as array - Multi select box need array_walk_recursive check each item.
The code will convert all special character to UTF-8.
<?php
/*prevent Sql Injection*/
function _CleanInputChars(&$value){
    return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}
foreach ($_POST as $key => $value) {
/*Array*/
if(is_array($value)){
array_walk_recursive($value, "_CleanInputChars");
}
else{
/*Number,String*/
$_POST[$key] = _CleanInputChars($value);
}
}
foreach ($_GET as $key => $value) {
/*Array*/
if(is_array($value)){
array_walk_recursive($value, "_CleanInputChars");
}
else{
/*Number,String*/
$_GET[$key] = _CleanInputChars($value);
}
}
/*END prevent Sql Injection*/
?>

Example for Mysqli
Mysli '#' mean mysql no execute query after '#'.
Form username input:
nouser" or 1=1#
nouser' or 1=1#

Thứ Tư, 22 tháng 2, 2017

PHP warning session_start() - SOLVED


Description: PHP Warning:  session_start(): Cannot send session cache limiter - headers already sent
It's hard to check. So wast much time to debug.

On internet guide is session_start() is the top code+html : YES- this right
BUT: it's really hard debug on big site working.-> waste much time

The simple way is: 
1. Create 1 file testsession.php 
Add code
<?php  
session_start();
error_reporting(E_ALL ^ E_NOTICE);
?>
2. Upload to host
3. Run file testsession.php 
Check view error_log at root folder: No error_log, NO waring -> is GOOD status. This mean is OK
4. Check many parts source: this is the important step
Check above HTML
Check head
Check body
....
I found issue so easy.
Good luck

Thứ Năm, 1 tháng 12, 2016

PHP prevent form resubmit - SOLVED

Detail: Form submit. Page reload -> form resubmit again. I did take 3 days to find on google and SOLVED. So thanks all.
Solution: catch session at Server

CODE:
Server PHP
if (isset($_POST['formid']) && isset($_SESSION['formid']) && $_POST["formid"] == $_SESSION["formid"])
{
$_SESSION["formid"] = '';
$isFirstSubmit = true;
}else{
$isFirstSubmit = false;
}
/*FormSession validate submit*/
$_SESSION["formid"] = md5(rand(0,10000000));

At Form Add 
<form>
<input type="hidden" name="formid" value="<?php echo htmlspecialchars($_SESSION["formid"]); ?>"/>
</form>

Chủ Nhật, 23 tháng 8, 2015

.htaccess directory index

Description: Use .htacccess file index priority index.php, index.html, index.asp...
Solution: .htaccess file
Add code:
DirectoryIndex index.php index.html

Chủ Nhật, 23 tháng 2, 2014

Joomla ERROR PAGE - Redirect to URL

File hiệu chỉnh: Hiệu chỉnh file templates/yourtemplate/error.php
Nguyên tắc hoạt động: khi jooml báo lỗi link sẽ chạy file error.php, lúc này file sẽ trỏ đến 1 bài viết trong joomla.
Mục đích: để giữ template hiển thị và style.
So sánh: có lợi hơn rất là là nhiều nếu redirect bằng html header <meta http-equiv="refresh" content="0; url=http://example.com/" />, lý do khi gặp file php là đang chạy ở server và redirect. Còn nếu chạy HTML Redirect thì lúc về đến client rồi mới redirect - lúc này người dùng sẽ thấy được trang trắng khoảng 1s.
Source:
<?php
$location=$this->baseurl.'/index.php?option=com_content&view=article&id=126&Itemid=718';
header("Location:".$location); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>

Thứ Sáu, 1 tháng 11, 2013

Xem version PHP và đường dẫn tuyệt đối trên host

Một vài trường hợp cần xem đường dẫn tuyệt đối trên host
Và xem version PHP
Download file sau: phpinfo.php
Link dự phòng:
https://drive.google.com/file/d/0B785Epf6q5XlVENZeFpJTUJIc0k/edit?usp=sharing


Thứ Ba, 27 tháng 8, 2013

php get first image tag from string

<?php 
preg_match('/(<img[^>]+>)/i', $item['content'], $matches);
echo $matches[0]; 
?>

Thứ Bảy, 24 tháng 8, 2013

Thêm module JModuleHepler

Mô tả:
Bình thường xử lý Joomla tự động tạo <jdoc:include type="modules" name="left" style="xhtml" />, cấu trúc này không phải lúc nào cũng hoạt động đặc biệt là lúc cần thêm module vào 1 component.
Cách sử dụng:
Sử dụng đoạn code trong mã PHP của component, index.php, module  ... bất cứ ở đâu trong PHP
<?php
    //Module position VM-Category-Top
    jimport( 'joomla.application.module.helper' );
    $module = JModuleHelper::getModules( 'VM-Category-Top' );
    if(count($module)){?>
    <div id="ja-VM-Category-Top">
        <?php
        $attribs['style'] = 'xhtml';
        foreach ($module as $new_module){
            echo JModuleHelper::renderModule($new_module, $attribs);
        }?>      
        <div class="clr"></div>
    </div>
<?php } ?>