Showing posts with label PHP INI_SET. Show all posts
Showing posts with label PHP INI_SET. Show all posts

Tuesday, 18 September 2018

Change the user agent string in PHP

Most of PHP's file functions can be used to open local files as well as remote files via HTTP etc. By default the user agent string passed when making an HTTP request is an empty string but it is possible in PHP to change the user agent string to something else. This post looks at how to do this.
For example, if you were going to download a copy of an RSS file you could do this using the file_get_contents() function, where $rss is the full URL to the RSS file:
$xml = file_get_contents($rss);
It may be better to use CURL or other methods for a request like this which have better error handling etc, but that's not the point of this post :)
The above request would look like this in an Apache access log file:
192.168.1.15 - - [01/Oct/2008:21:52:43 +1300] "GET / HTTP/1.0" 200 5194 "-" "-"
The last two "-" items in the above line are the referrer URL (when specified) and the user agent string (when specified). If they were not specified by the browser/script/etc then they are represented by -
PHP has an ini setting called "user_agent" which lets you specify the user agent string when making these sorts of HTTP requests. It can be specified anywhere - httpd.conf, php.ini, .htaccess and in your scripts.
To set the user_agent value in your PHP script use the ini_set function as in the following example. This sets the user-agent string to Firefox 3.0.3 in Windows, which is the current version of Firefox for Windows at the time of this post:
ini_set('user_agent', 
  'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3');
Now when we make the same request as at the start of this post, the line in the Apache log file would look like this:
192.168.1.15 - - [01/Oct/2008:21:54:29 +1300] "GET / HTTP/1.0" 200 5193
   "-" "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3"
For the purposes of readability, I've split the line into two but it would normally appear all on one line in the log file. You can see that while the HTTP referrer is still empty, the user-agent string is now populated with the value we specified.

Related posts:

Saturday, 8 September 2018

Changing the timezone with PHP

Since PHP 5.1.0 it has been possible to change the default timezone that PHP uses using the date.timezone configuration, which can be set for the entire server in php.ini or httpd.conf, on a virtualhost by virtualhost basis, or using the ini_set() function. This post looks at how to change the timezone in PHP using the ini_set function.
Using ini_set() in your script to set the current timezone to e.g. the timezone that is used in Los Angeles, you would do this:
ini_set('date.timezone', 'America/Los_Angeles');
All subsequent requests to date and time functions would use America/Los Angeles as the timezone.
If you need to do this in your scripts, for example if the web server you are using is in a different timezone from the dates and times you wish to use in your scripts, then you should ensure the call is made at the very start of your scripts (or, if you can, move the setting out to an .htaccess file).
A complete list of the timezones available can be found here: http://www.php.net/manual/en/timezones.php
While looking at the various settings etc in the PHP manual I also came across the date_default_timezone_set functionwhich I have not used personally myself. I'll try this function out at a later date and post about using that as well.

Related posts:

Wednesday, 3 June 2015

PHP: Parsing an complex array Parsing an very complex array into simple user friendly view

<?php
ini_set('display_errors', E_ALL);
ini_set('memory_limit', '2048M');
ini_set('max_execution_time', '0');
include_once("xml2array.php"); //include class file

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("db_zorg") or die(mysql_error());


global $db;
$db=true;   //true if you want to run query

$filename="~ALL_BHC~.xml";
$content = file_get_contents($filename); //Get the xml data as string

// $content = iconv("utf-16","utf-8",$content);
$content=str_replace("®","",$content);
$content=str_replace("- <","<",$content);

echo "<pre>";
$clsXML = new XMLToArray;
//Create an instance of class XMLToArray
$data= $clsXML->xml2array($content,1); //(String content, 1 or 0) If this is 0 the function will get the array value without attributes as well as the tag values.


function getValue($arr) {
 $value="";
 if(isset($arr['value']) && $arr['value']!='') {
  $value=value(utf8_decode($arr['value']));
 }
 return $value;
} 



// If this is 1 the function will get the array value with attributes.
$bhc=array();
$bhc=$data['MEDIQUEST']['BHC'];  //Getting Member array


// echo "<pre>Arr(arr) = "; print_r($bhc); echo "</pre>"; exit;


$ctr = 1;
foreach($bhc as $bhcChild) {

 $bhcId=$bhcChild['attr']['BHCID'];
 
 // if($bhcId != '54100') { $ctr++; continue; } //-- mjv
 
 $check_member = "SELECT id FROM members where bhcid = '".$bhcId."'";
 $rs_check_member = mysql_query($check_member);
 $count_member = mysql_num_rows($rs_check_member);
 
 $row = mysql_fetch_assoc($rs_check_member);
 $member_id = $row['id'];
 if($member_id <= 0) { echo "BHCID-".$bhcId; continue; }
 
 $bhcChildArray=array();
 foreach($bhcChild as $k=>$v) {
  $bhcChildArray[strtolower($k)]=getValue($v);
 }
 
 $arr = $bhcChild;
 echo "<br/><br/>Counter > $ctr -- BHCID > ".$bhcId."($member_id) -- ".$arr['NAAM']['value'];
 
 if(isset($arr['SPECIALISMEN'])) {
  $arr_speciality = $arr['SPECIALISMEN'];
  
  foreach($arr_speciality as $key2 => $speciality) {
  
   foreach($speciality as $key3 => $val3) {
   
    $specialist_id = 0;
    if(isset($val3['attr'])) {
     
     $specialismeId = $val3['attr']['SPECIALISMEID'];
     // echo "<br/>-----Speciality If > $specialismeId($member_id) -- ".$val3['NAAM']['value']; 
     
     $sql_check_speciality = "SELECT id FROM specialities where member_id = '".$member_id."' AND specialismeid = '".$specialismeId."'";
     $rs_check_speciality = mysql_query($sql_check_speciality);
     $count_check_speciality = mysql_num_rows($rs_check_speciality);
     if($count_check_speciality > 0) {
      $row_check_speciality = mysql_fetch_assoc($rs_check_speciality);
      $specialist_id = $row_check_speciality['id'];
     }
    }
    else {
     if(!isset($val3['SPECIALISMEID'])) { continue; }
     $specialismeId = $val3['SPECIALISMEID'];
     // echo "<br/>-----Speciality Else > $specialismeId($member_id) -- ".$speciality['NAAM']['value']; 
     
     $sql_check_speciality = "SELECT id FROM specialities where member_id = '".$member_id."' AND specialismeid = '".$specialismeId."'";
     $rs_check_speciality = mysql_query($sql_check_speciality);
     $count_check_speciality = mysql_num_rows($rs_check_speciality);
     if($count_check_speciality > 0) {
      $row_check_speciality = mysql_fetch_assoc($rs_check_speciality);
      $specialist_id = $row_check_speciality['id'];
     }
     // echo "<pre>Arr(arr) = "; print_r($speciality); echo "</pre>"; exit;
    }

    if((isset($val3['AANDOENINGEN'])) && ($specialist_id > 0)) {
     $treatments = $val3['AANDOENINGEN']; 
     foreach($treatments as $key4 => $treatment) {
      if(isset($treatment['attr'])) {
       $treatment_id = $treatment['attr']['AANDOENINGID'];
       // echo "<br/>----------Treatment MultiS > $treatment_id($specialist_id) -- ".$treatment['NAAM']['value']; 
       getTreatment($treatment, $specialismeId, $specialist_id, $member_id);
       // echo "<pre>Arr(arr) = "; print_r($treatment); echo "</pre>";
      }
      foreach($treatment as $key5 => $val5) {
       if(isset($val5['attr'])) { 
        $treatment_id = $val5['attr']['AANDOENINGID'];
        // echo "<br/>-----------Treatment MultiM > $treatment_id($specialist_id) -- ".$val5['NAAM']['value']; 
        getTreatment($val5, $specialismeId, $specialist_id, $member_id);
       }
      }
     }
    }else {
      if($specialist_id > 0) {
      if(isset($speciality['AANDOENINGEN']['AANDOENING'])) {
       $treatments = $speciality['AANDOENINGEN']['AANDOENING']; 
       if(isset($treatments['attr'])) {
        $treatment_id = $treatments['attr']['AANDOENINGID'];
        // echo "<br/>----------Treatment Single > $treatment_id($specialist_id) -- ".$treatments['NAAM']['value']; 
        getTreatment($treatments, $specialismeId, $specialist_id, $member_id);
       }
       else {

        foreach($treatments as $key4 => $treatment) {
         if(isset($treatment['attr'])) {
          $treatment_id = $treatment['attr']['AANDOENINGID'];
          // echo "<br/>----------Treatment MultiS2 > $treatment_id($specialist_id) -- ".$treatment['NAAM']['value']; 
          getTreatment($treatment, $specialismeId, $specialist_id, $member_id);
          // echo "<pre>Arr(arr) = "; print_r($treatment); echo "</pre>";
         }
        }
       }
      }
     }
    }
    // Ends -- isset($val3['AANDOENINGEN']
   }
  }
 }
 
 $ctr++;
}
//Outer foreach end 


mysql_query("update treatments set title = 'Reumatoide artritis' where title = 'Reumatoïde artritis'"); //-- Reumatoïde artritis

mysql_query("update treatments set title = 'Langzaamwerkende schildklier (Hypothyreoidie)' where title = 'Langzaamwerkende schildklier (Hypothyreoïdie)'"); //-- Langzaamwerkende schildklier (Hypothyreoïdie)

mysql_query("update treatments set title = 'Paniekstoornissen en fobieen' where title = 'Paniekstoornissen en fobieën'"); //-- Paniekstoornissen en fobieën

mysql_query("update treatments set title = 'Rontgen onderzoek' where title = 'Röntgen onderzoek'"); //-- Röntgen onderzoek

mysql_query("update treatments set title = 'Snelwerkende schildklier (Hyperthyreoidie)' where title = 'Snelwerkende schildklier (Hyperthyreoïdie)'"); //-- Snelwerkende schildklier (Hyperthyreoïdie)

mysql_query("update treatments set title = 'Sjogren, syndroom van' where title = 'Sjögren, syndroom van'"); //-- Sjögren, syndroom van


echo "<br/><br>Total Records: ".count($bhc);

function getTreatment($aandoeningChild, $specialismeid, $specialist_id, $member_id) {

 global $db;
 
 $aandoeningChildArray=array();
 $aandoeningId=$aandoeningChild['attr']['AANDOENINGID'];
 
 foreach($aandoeningChild as $key=>$val)
 {
  if($key=='attr') continue;
  
  $aandoeningChildArray[strtolower($key)]=getValue($val);
 }
  
 extract($aandoeningChildArray);
 //INSERT DATA IN THE TREATMENT TABLE
 
 $sql_check_treatment = "SELECT id FROM treatments where specialismeid = '".$specialismeid."' AND aandoeningid = '".$aandoeningId."' AND clinic_id = '".$member_id."'";
 $rs_check_treatment = mysql_query($sql_check_treatment);
 $count_check_treatment = mysql_num_rows($rs_check_treatment);
 
 if($count_check_treatment > 0) {
 
  $sql="UPDATE treatments set 
  title='".$naam."',
  specialist_id='".$specialist_id."',
  wachttijdpoli='".$wt_polikliniek."',
  wt_behandeling='".$wt_behandeling."',
  wachttijdopname='".$wt_retro."',
  wachttijddag='".$wt_status."',
  kwic_eindscore='".$kwic_eindscore."',
  kwic_septielscore='".$kwic_septielscore."',
  kwic_transpasp='".$kwic_transpasp."',
  kwic_transpaspcat='".$kwic_transpaspcat."',
  rodevlag='".$rodevlag."',
  rodevlagtoelichting='".$rodevlagtoelichting."',
  modified='".date("Y-m-d H:i:s")."'
  WHERE aandoeningid='".$aandoeningId."' AND specialismeid = '".$specialismeid."' AND clinic_id = '".$member_id."'";
 }
 else {
 
  $sql="INSERT INTO treatments set 
  title='".$naam."',
  clinic_id='".$member_id."',
  specialist_id='".$specialist_id."',
  specialismeid='".$specialismeid."',
  wachttijdpoli='".$wt_polikliniek."',
  wt_behandeling='".$wt_behandeling."',
  wachttijdopname='".$wt_retro."',
  wachttijddag='".$wt_status."',
  kwic_eindscore='".$kwic_eindscore."',
  kwic_septielscore='".$kwic_septielscore."',
  kwic_transpasp='".$kwic_transpasp."',
  kwic_transpaspcat='".$kwic_transpaspcat."',
  rodevlag='".$rodevlag."',
  rodevlagtoelichting='".$rodevlagtoelichting."',
  created='".date("Y-m-d H:i:s")."',
  modified='".date("Y-m-d H:i:s")."',
  aandoeningid='".$aandoeningId."'";
 }
 
 if($db) {
  // echo $sql;
  mysql_query($sql);
 } 
 return '';
}

### Converts Database text value HTML compatible for text fields
### Don't use this function in case of getting values from textarea or editor as it strip the tags
function value($val = '', $default = '', $arr = '') {

 $numargs = func_num_args(); // call_user_func_array - Call a user function given with an array of parameters

 if(gettype($arr) == 'object') {
  $arr = get_object_vars($arr);
 }
 
 if($numargs > 2) {
  if(is_array($arr) && (count($arr) > 0)) {
   if( (!empty($val)) && (isset($arr[$val])) && (trim($arr[$val] != '')) ) {
    return get_absolute_value($arr[$val], $default);
   }
  }
  return get_absolute_value($default);
 }
 return get_absolute_value($val, $default);
}

### Converts Database text value HTML compatible for text fields
function get_absolute_value($val ='', $default = '') {
 if(!empty($val)) {
  if(is_string($val) ) {

   // For Textbox and other fields
   return htmlspecialchars(strip_tags(stripslashes(trim($val))), ENT_QUOTES);
  }
  return $val;
 }
 else {
  if( ($val === 0) || ($val === '0') ) {
   return $val;
  }
  return $default;
 }
 return false;
}
?>


<?php echo $this->Html->css('dataTable'); ?> 

<?php echo $this->Html->script('dataTables/jquery.dataTables'); ?> 
<?php echo $this->Html->script('dataTables/colResizable.min'); ?> 

<?php echo $this->Html->script('jBreadCrumb.1.1'); ?> 
<?php echo $this->Html->script('cal.min'); ?> 
<?php echo $this->Html->script('jquery.collapsible.min'); ?> 
<?php echo $this->Html->script('jquery.ToTop'); ?> 
<?php echo $this->Html->script('jquery.listnav'); ?> 
<?php echo $this->Html->script('jquery.sourcerer'); ?> 

<?php echo $this->Html->script('wysiwyg/jquery.wysiwyg'); ?> 
<?php echo $this->Html->script('wysiwyg/wysiwyg.image'); ?> 
<?php echo $this->Html->script('wysiwyg/wysiwyg.link'); ?> 
<?php echo $this->Html->script('wysiwyg/wysiwyg.table'); ?> 

<?php echo $this->Html->script('flot/jquery.flot'); ?> 
<?php echo $this->Html->script('flot/jquery.flot.pie'); ?> 
<?php echo $this->Html->script('flot/excanvas.min'); ?> 

<?php echo $this->Html->script('selectunselect'); ?> 

<?php echo $this->Html->css('facebox'); ?> 
<?php echo $this->Html->script('facebox'); ?> 

<?php echo $this->Html->css('colorboxx'); ?> 
<?php echo $this->Html->script('jquery.colorboxx'); ?> 


<?php
 if(isset($status) && trim($status)!=""){
  $url = array($status);
  $this->Paginator->options(array('url' => $url));
 }
?>


<script type="text/javascript">
<!--
function del(field) {
  if(!anyChecked(field)) {
   // alert('Please select atleast one record to perform any action.');
  jAlert('Please select atleast one record to perform any action.', 'Alert::MyDementiaCare');
   return false;
  } else {
  if(jQuery('#CareplusSuggestionStatus').val() != '') {
    if(jQuery('#CareplusSuggestionStatus').val() == 'delete'){
    if(!confirm("Are you sure you want to perform this action?")){
     return false;
    }
    } else {
     return true;
    }
  }
  else {
   jAlert('Please select any option from drop down to perform any action.', 'Alert::MyDementiaCare');
   return false;
  }
  }
}
//-->
</script>

<script type="text/javascript">
<!--
function delete_suggestion(suggestion_id) {
 if(confirm("Are you sure you want to perform this action?")) {
 
  var actionurl = '<?php echo SITE_URL; ?>'+"admin/careplus/profile/delete_suggestion/"+tip_id+"/<?php echo date("Ymdhis"); ?>";
  
  jQuery.ajax({
   type: 'GET',
   url : actionurl,
   data:{},
   datatype:"html", 
    success: function(data, status) {
     var div = "#tr_"+suggestion_id;
     jQuery(div).html('');
    },
    error: function (data, status, e) {
     // alert('Status: '+status+'; Server Error: '+e+'; Action URL : '+actionurl);
    }
  });
 }
}
//-->
</script>



<?php echo $this->Form->create(null ,array('url'=>'/admin/careplus/profile/suggestions/'.$tip_id, 'class'=>'mainForm')); ?>

<div class="content">
<div class="title" style=""><h5>Manage Suggestions</h5></div>

<div class="users index">
 
<?php 
if (($this->Session->check('Message.flash'))) {
 echo $this->Session->flash('flash', array('element' => 'flash'));
}
?>
 

<div align="right">
<a class="btnIconLeft mt5" href="<?php echo SITE_URL; ?>admin/careplus/profile/add_suggestion/<?php echo $tip_id."/".date("YmdHis"); ?>"><img class="icon" alt="" src="<?php echo SITE_URL; ?>img/icons/dark/presentation.png"><span>Add New Suggestion</span></a>
</div>

 
<div class="table">
            <div class="head"><h5 class="iFrames">Suggestions</h5></div>
            <div class="dataTables_wrapper" id="example_wrapper">
   <div class="" style="visibility:hidden;">
    <div class="dataTables_filter" id="example_filter">
     <label>Search: <input type="text" placeholder="type here...">
     <div class="srch"></div>
     </label>
    </div>
   </div>
    
   <table cellspacing="0" cellpadding="0" border="0" id="" class="display">
   
                <thead>
                    <tr>
      <th rowspan="1" colspan="1"  width="5%" id="none" class="none;">
        
       <?php echo $this->Form->text('CareplusSuggestion.all', array('type'=>'checkbox', 'id'=>'data[CareplusSuggestion][all]', 'style' => 'display:block;', 'onclick'=>"selectDeselect('data[CareplusSuggestion][id][]', this.name);"));?>
       
      </th>
      <th class="ui-state-default" rowspan="1" colspan="1" width="3%" style="cursor:auto;">
       <div style="padding-right:8px;">#</div>
      </th>
      <th class="ui-state-default" rowspan="1" colspan="1">Suggestion</th>
      <th class="ui-state-default" rowspan="1" colspan="1" style="width:110px; text-align:center;">Approval Status</th>
     </tr>
                </thead>
    
    <tbody>    
    <?php
    $rowcount = 0;
    $countTips = count($tips);
    if($countTips > 0){
    foreach ($tips AS $row) {
    $index = ((($page-1)*$limit) + ($rowcount+1));
    $suggestion_id = $tips[$rowcount]['CareplusSuggestion']['id'];
    ?>
    <tr id="tr_<?php echo $suggestion_id; ?>" class="gradeA <?php if($rowcount%2==0) echo 'odd'; else echo 'even';?>">
     <td align="center">
      
      <?php echo $this->Form->text('CareplusSuggestion.id][', array('type'=>'checkbox', 'style' => 'display:block;', 'value'=>$tips[$rowcount]['CareplusSuggestion']['id'],  'onclick'=>"checkSelection('data[CareplusSuggestion][id][]', 'data[CareplusSuggestion][all]')"));?>
      
     </td>
     <td align="center"><?php echo $index;?></td>
     <td>
     <?php
      echo nl2br($tips[$rowcount]['CareplusSuggestion']['description']);
     ?>
     
     </td>
     <td align="center">
      <a id="a_<?php echo $tips[$rowcount]['CareplusSuggestion']['id']; ?>" title="Click to change status" href="javascript:void(0);" onclick="change_status('<?php echo $tips[$rowcount]['CareplusSuggestion']['id']; ?>')"><?php echo ($tips[$rowcount]['CareplusSuggestion']['approved'] == "1") ? "Approved" : "Un Approved"; ?></a>
     </td>
    </tr>
    <?php
     $rowcount++;
    }
    }
    ?>
    </tbody>
    
    </table>
    
    
    <?php
    if($countTips > 0) {
    ?>
    
    <?php echo $this->element('admin/paginate')?>

    <div class="fg-toolbar ui-toolbar  ui-corner-bl ui-corner-br ui-helper-clearfix">
     <div id="example_length" class="dataTables_length">
      <?php
       echo $this->Form->input('CareplusSuggestion.status', array(
        'options' => $mode,
        'empty' => '(action)',
        'label' => false,
        'class' => 'validate[required]',
        'error' => false,
        'div' => false,
        'style' => 'width:95px;'
       ));
      ?>
          <input type="submit" value="Submit" class="redBtn" onclick="return del('data[CareplusSuggestion][id][]')" name="data[CareplusSuggestion][submit]">
     </div>
      <div class="dataTables_paginate fg-buttonset ui-buttonset fg-buttonset-multi ui-buttonset-multi paging_full_numbers" id="example_paginate">
       
     </div>
    </div> 

    <?php
    }
    else {
    ?>
    <div class="fg-toolbar ui-toolbar ui-widget-header ui-corner-bl ui-corner-br ui-helper-clearfix">
     <div class="dataTables_length" id="example_length">
      <label>No Records Available Yet.</label></div>
    </div>
    <?php
    }
    ?>
    
   </div>
        </div> 
</div>

</div>

<?php echo $this->Form->end(); ?>

<script type="text/javascript">
<!--
function change_status(suggestion_id) {
 if(confirm("Are you sure you want to change approval status?")) {
 
  var actionurl = '<?php echo SITE_URL; ?>'+"admin/careplus/profile/change_approval_status/"+suggestion_id+"/<?php echo date("Ymdhis"); ?>";
  
  jQuery.ajax({
   type: 'GET',
   url : actionurl,
   data:{},
   datatype:"html", 
    success: function(data, status) {
     var div = "#a_"+suggestion_id;
     jQuery(div).html(data);
    },
    error: function (data, status, e) {
     // alert('Status: '+status+'; Server Error: '+e+'; Action URL : '+actionurl);
    }
  });
 }
}
//-->
</script>

<?php /*
AgreeYa Solutions
R Systems
Mind Genies
Virtual Employee
Classic Informatics Private Limited
Xicom Technologies LLC
A-1 Technology 
Sebiz Infotech 
SmartData Enterprises 
Infopro India Pvt. Ltd. 
Binary Semantics Ltd. 
BrickRed Technologies Pvt. Ltd. 
Ishir Infotech
MagicBricks.com 
Headstrong 
Flexsin Technologies Pvt. Ltd


Authorgen Technologies
Nucleus Software Exports Ltd
InterraIT
Konstant Infosolutions Pvt. Ltd.
Sapple Systems (P) Ltd.
Octal Info Solution Pvt. Ltd. 
FCS Software Solutions Ltd.
Rosmerta Technologies Limited
Lime Labs
Brain Technosys Pvt. Ltd.
SDP Labs
Avalon Information Systems Pvt. Ltd.
SUVI Information Systems
Saviance Technologies
Nagarro Software Pvt. Ltd.
Sapient Corporation
Percept Knorigin
Sisoft Technologies
DivPlayers
4th Quarter Technologies
IndiaMart InterMESH Ltd.
RC & M 
Aar Ess Remedies Pvt. Ltd.
SAIC
Fiserv
Xavient Information Systems
Halosys Technologies Inc.
Kale Consultants Ltd.
BrainPulse Technologies
Maniks Systems Pvt Ltd.
Kairaus Software Pvt. Ltd.
Grey Matter India Technologies Pvt. Ltd.
Icreon Communications
Neuronimbus Software Service Pvt. Ltd.
Broadway Infotech Pvt. Ltd.
Mosaic ITES Services Pvt. Ltd.
Shriv ComMedia Solutions Pvt. Ltd
UG Software Technologies Pvt. Ltd.
eCognysys Technologies
Caneum India Pvt. Ltd.
Vinove Software and Services Pvt. Ltd.
ITCons e- Solutions Pvt. Ltd.
Compare Infobase Pvt. Ltd.
Agile Technosys
Symphony Services
*/