function isJson(str) { try { JSON.parse(str); } catch (e) { return false; } return true; }
Thursday, January 30, 2020
Validate given string is JSON
Convert a JavaScript Object to FormData Object
/* takes a {} object and returns a FormData object */
function objectToFormData(obj, form, namespace) {
var fd = form || new FormData();
var form_key;
for (var property in obj) {
if (obj.hasOwnProperty(property) && property != '$$hashKey') {
if (namespace) {
form_key = namespace + '[' + property + ']';
} else {
form_key = property;
}
/*if the property is an object, but not a File,*/
/*use recursivity.*/
if (obj[property] instanceof Date) {
fd.append(form_key, obj[property].toISOString());
} else if (typeof obj[property] === 'object' && !(obj[property] instanceof File)) {
objectToFormData(obj[property], fd, form_key);
} else {
/*if it's a string or a File object*/
if (!empty(obj[property])) {
fd.append(form_key, obj[property]);
}
}
}
}
return fd;
}
function objectToFormData(obj, form, namespace) {
var fd = form || new FormData();
var form_key;
for (var property in obj) {
if (obj.hasOwnProperty(property) && property != '$$hashKey') {
if (namespace) {
form_key = namespace + '[' + property + ']';
} else {
form_key = property;
}
/*if the property is an object, but not a File,*/
/*use recursivity.*/
if (obj[property] instanceof Date) {
fd.append(form_key, obj[property].toISOString());
} else if (typeof obj[property] === 'object' && !(obj[property] instanceof File)) {
objectToFormData(obj[property], fd, form_key);
} else {
/*if it's a string or a File object*/
if (!empty(obj[property])) {
fd.append(form_key, obj[property]);
}
}
}
}
return fd;
}
Check given variable is Empty ?
/* check variable contain empty values or not */
function empty(data) {
if (typeof(data) == 'number' || typeof(data) == 'boolean') {
return false;
}
if (typeof(data) == 'undefined' || data === null) {
return true;
}
if (typeof(data.length) != 'undefined') {
/*if(/^[\s]*$/.test(data.toString())) {
return true;
}*/
return data.length == 0;
}
if (data instanceof File) {
return false;
}
var count = 0;
for (var i in data) {
/* if (data.hasOwnProperty(i)) { */
count++;
/* } */
}
return count == 0;
}
function empty(data) {
if (typeof(data) == 'number' || typeof(data) == 'boolean') {
return false;
}
if (typeof(data) == 'undefined' || data === null) {
return true;
}
if (typeof(data.length) != 'undefined') {
/*if(/^[\s]*$/.test(data.toString())) {
return true;
}*/
return data.length == 0;
}
if (data instanceof File) {
return false;
}
var count = 0;
for (var i in data) {
/* if (data.hasOwnProperty(i)) { */
count++;
/* } */
}
return count == 0;
}
Wednesday, January 29, 2020
Send Post Request without response
function post_without_wait12($url, $params)
{
foreach ($params as $key => &$val) {
if (is_array($val)) $val = implode(',', $val);
$post_params[] = $key.'='.urlencode($val);
}
$post_string = implode('&', $post_params);
$parts=parse_url($url);
$fp = fsockopen($parts['host'],
isset($parts['port'])?$parts['port']:80,
$errno, $errstr, 30);
$out = "POST ".$parts['path']." HTTP/1.1\r\n";
$out.= "Host: ".$parts['host']."\r\n";
$out.= "Content-Type: application/x-www-form-urlencoded\r\n";
$out.= "Content-Length: ".strlen($post_string)."\r\n";
$out.= "Connection: Close\r\n\r\n";
if (isset($post_string)) $out.= $post_string;
fwrite($fp, $out);
fclose($fp);
}
{
foreach ($params as $key => &$val) {
if (is_array($val)) $val = implode(',', $val);
$post_params[] = $key.'='.urlencode($val);
}
$post_string = implode('&', $post_params);
$parts=parse_url($url);
$fp = fsockopen($parts['host'],
isset($parts['port'])?$parts['port']:80,
$errno, $errstr, 30);
$out = "POST ".$parts['path']." HTTP/1.1\r\n";
$out.= "Host: ".$parts['host']."\r\n";
$out.= "Content-Type: application/x-www-form-urlencoded\r\n";
$out.= "Content-Length: ".strlen($post_string)."\r\n";
$out.= "Connection: Close\r\n\r\n";
if (isset($post_string)) $out.= $post_string;
fwrite($fp, $out);
fclose($fp);
}
Is PHP Session Start ?
if ( pk_is_session_started() === FALSE ) session_start();
function pk_is_session_started()
{
if ( php_sapi_name() !== 'cli' ) {
if ( version_compare(phpversion(), '5.4.0', '>=') ) {
return session_status() === PHP_SESSION_ACTIVE ? TRUE : FALSE;
} else {
return session_id() === '' ? FALSE : TRUE;
}
}
return FALSE;
}
function pk_is_session_started()
{
if ( php_sapi_name() !== 'cli' ) {
if ( version_compare(phpversion(), '5.4.0', '>=') ) {
return session_status() === PHP_SESSION_ACTIVE ? TRUE : FALSE;
} else {
return session_id() === '' ? FALSE : TRUE;
}
}
return FALSE;
}
Tuesday, January 28, 2020
WordPress Check Dependent Plugin Is Installed ?
register_activation_hook( __FILE__, 'pkLm_plugin_activate' );
function pkLm_plugin_activate(){
// Require parent plugin
if ( ! is_plugin_active( `'plugin_folder/plugin_file'` ) and current_user_can( 'activate_plugins' ) ) {
// Deactivate the plugin
deactivate_plugins(__FILE__);
// Throw an error in the wordpress admin console
$error_message = __('This plugin requires <a href="#">`Dependent Plugin Name`</a> plugin to be active!');
die($error_message);
}
}
function pkLm_plugin_activate(){
// Require parent plugin
if ( ! is_plugin_active( `'plugin_folder/plugin_file'` ) and current_user_can( 'activate_plugins' ) ) {
// Deactivate the plugin
deactivate_plugins(__FILE__);
// Throw an error in the wordpress admin console
$error_message = __('This plugin requires <a href="#">`Dependent Plugin Name`</a> plugin to be active!');
die($error_message);
}
}
Remove index.php from URL
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Subscribe to:
Posts (Atom)