Wednesday, 20 June 2018

To display hyperlinks in cdetailview in yii that is to display uploaded files in yii

<?php $path = Yii::app()->baseUrl."/uploads/"; ?>  // First we have to Declare the path before the attaching the filename in the link this is the key step. 

/* then after we can defile the attribute as follows */
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'lan_no',
array(
'label' => 'Uploaded Image',
'type'=>'raw',
'value' => CHtml::link(CHtml::encode($model->image), $path .$model->image,array("target"=>"_blank"))
),
'man_code',
'vil_code',
'loc_name',
'sur_no',
'tsno',
'blo_name',
'nala_rem',
),
)); ?>

That's it if you want you can remove the target="_blank" option 

To display uploaded files in Cgridview ( in admin view ) in yii

<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'works-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'department',
'descofwork',
'typeofwork',
'workstatusrem',
array(
'header' => 'WORK Agreement Copy',         // Header  for the column
'name' => 'workagreement',                           // Name of the database field
'value' => '($data->workagreement === "NIL" ? $data->workagreement :CHtml::link($data->workagreement,Yii::app()->baseUrl."/uploads/".$data->workagreement,array("target"=>"_blank")))',
'type'=>'raw',
),
'place',
'village',
array(
'class'=>'CButtonColumn',
'template'=>$format,
),
),
)); ?>

Tuesday, 20 February 2018

How To Install the Apache Web Server on Ubuntu 16.04

Introduction

The Apache HTTP server is the most widely-used web server in the world. It provides many powerful features including dynamically loadable modules, robust media support, and extensive integration with other popular software.
In this guide, we'll discuss how to install an Apache web server on your Ubuntu 16.04 server.

Prerequisites

Before you begin this guide, you should have a regular, non-root user with sudo privileges configured on your server. Additionally, you will need to configure a basic firewall to block non-essential ports. You can learn how to configure a regular user account and set up a firewall for your server by following our initial server setup guide for Ubuntu 16.04.
When you have an account available, log in as your non-root user to begin.

Step 1: Install Apache

Apache is available within Ubuntu's default software repositories, so we will install it using conventional package management tools.
We will begin by updating the local package index to reflect the latest upstream changes. Afterwards, we can install the apache2 package:
  • sudo apt-get update
  • sudo apt-get install apache2
After confirming the installation, apt-get will install Apache and all required dependencies.

Step 2: Adjust the Firewall

Before we can test Apache, we need to modify our firewall to allow outside access to the default web ports. Assuming that you followed the instructions in the prerequisites, you should have a UFW firewall configured to restrict access to your server.
During installation, Apache registers itself with UFW to provide a few application profiles. We can use these profiles to simplify the process of enabling or disabling access to Apache through our firewall.
We can list the ufw application profiles by typing:
  • sudo ufw app list
You should get a listing of the application profiles:
Output
Available applications: Apache Apache Full Apache Secure OpenSSH
As you can see, there are three profiles available for Apache:
  • Apache: This profile opens only port 80 (normal, unencrypted web traffic)
  • Apache Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
  • Apache Secure: This profile opens only port 443 (TLS/SSL encrypted traffic)
For our purposes, we will allow incoming traffic for the Apache Full profile by typing:
  • sudo ufw allow 'Apache Full'
You can verify the change by typing:
  • sudo ufw status
You should see HTTP traffic allowed in the displayed output:
Output
Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere Apache Full ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Apache Full (v6) ALLOW Anywhere (v6)
As you can see, the profile has been activated to allow access to the web server.

Step 3: Check your Web Server

At the end of the installation process, Ubuntu 16.04 starts Apache. The web server should already be up and running.
We can check with the systemd init system to make sure the service is running by typing:
  • sudo systemctl status apache2
Output
● apache2.service - LSB: Apache2 web server Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled) Drop-In: /lib/systemd/system/apache2.service.d └─apache2-systemd.conf Active: active (running) since Fri 2017-05-19 18:30:10 UTC; 1h 5min ago Docs: man:systemd-sysv-generator(8) Process: 4336 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS) Process: 4359 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCESS) Tasks: 55 Memory: 2.3M CPU: 4.094s CGroup: /system.slice/apache2.service ├─4374 /usr/sbin/apache2 -k start ├─4377 /usr/sbin/apache2 -k start └─4378 /usr/sbin/apache2 -k start May 19 18:30:09 ubuntu-512mb-nyc3-01 systemd[1]: Stopped LSB: Apache2 web server. May 19 18:30:09 ubuntu-512mb-nyc3-01 systemd[1]: Starting LSB: Apache2 web server... May 19 18:30:09 ubuntu-512mb-nyc3-01 apache2[4359]: * Starting Apache httpd web server apache2 May 19 18:30:09 ubuntu-512mb-nyc3-01 apache2[4359]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message May 19 18:30:10 ubuntu-512mb-nyc3-01 apache2[4359]: * May 19 18:30:10 ubuntu-512mb-nyc3-01 systemd[1]: Started LSB: Apache2 web server.
As you can see above, the service appears to have started successfully. However, the best way to test this is to actually request a page from Apache.
You can access the default Apache landing page to confirm that the software is running properly. You can access this through your server's domain name or IP address.
If you are using DigitalOcean and do not have a domain name set up for your server, you can follow our guide how to set up a domain with DigitalOcean to set one up.
If you do not want to set up a domain name for your server, you can use your server's public IP address. If you do not know your server's IP address, you can get it a few different ways from the command line.
Try typing this at your server's command prompt:
  • hostname -I
You will get back a few addresses separated by spaces. You can try each in your web browser to see if they work.
An alternative is typing this, which should give you your public IP address as seen from another location on the internet:
  • sudo apt-get install curl
  • curl -4 icanhazip.com
When you have your server's IP address or domain, enter it into your browser's address bar:
http://server_domain_or_IP
You should see the default Ubuntu 16.04 Apache web page, which should look something like this:
Apache default page
This page is simply included to show that Apache is working correctly. It also includes some basic information about important Apache files and directory locations.

Step 4: Manage the Apache Process

Now that you have your web server up and running, we can go over some basic management commands.
To stop your web server, you can type:
  • sudo systemctl stop apache2
To start the web server when it is stopped, type:
  • sudo systemctl start apache2
To stop and then start the service again, type:
  • sudo systemctl restart apache2
If you are simply making configuration changes, Apache can often reload without dropping connections. To do this, you can use this command:
  • sudo systemctl reload apache2
By default, Apache is configured to start automatically when the server boots. If this is not what you want, you can disable this behavior by typing:
  • sudo systemctl disable apache2
To re-enable the service to start up at boot, you can type:
  • sudo systemctl enable apache2
Apache should now start automatically when the server boots again.

Step 5: Get Familiar with Important Apache Files and Directories

Now that you know how to manage the service itself, you should take a few minutes to familiarize yourself with a few important directories and files.

Content

  • /var/www/html: The actual web content, which by default only consists of the default Apache page you saw earlier, is served out of the /var/www/html directory. This can be changed by altering Apache configuration files.

Server Configuration

  • /etc/apache2: The Apache configuration directory. All of the Apache configuration files reside here.
  • /etc/apache2/apache2.conf: The main Apache configuration file. This can be modified to make changes to the Apache global configuration. This file is responsible for loading many of the other files in the configuration directory.
  • /etc/apache2/ports.conf: This file specifies the ports that Apache will listen on. By default, Apache listens on port 80 and additionally listens on port 443 when a module providing SSL capabilities is enabled.
  • /etc/apache2/sites-available/: The directory where per-site "Virtual Hosts" can be stored. Apache will not use the configuration files found in this directory unless they are linked to the sites-enabled directory (see below). Typically, all server block configuration is done in this directory, and then enabled by linking to the other directory with the a2ensite command.
  • /etc/apache2/sites-enabled/: The directory where enabled per-site "Virtual Hosts" are stored. Typically, these are created by linking to configuration files found in the sites-available directory with the a2ensite. Apache reads the configuration files and links found in this directory when it starts or reloads to compile a complete configuration.
  • /etc/apache2/conf-available/, /etc/apache2/conf-enabled/: These directories have the same relationship as the sites-available and sites-enabled directories, but are used to store configuration fragments that do not belong in a Virtual Host. Files in the conf-available directory can be enabled with the a2enconf command and disabled with the a2disconf command.
  • /etc/apache2/mods-available/, /etc/apache2/mods-enabled/: These directories contain the available and enabled modules, respectively. Files in ending in .load contain fragments to load specific modules, while files ending in .conf contain the configuration for those modules. Modules can be enabled and disabled using the a2enmod and a2dismod command.

Server Logs

  • /var/log/apache2/access.log: By default, every request to your web server is recorded in this log file unless Apache is configured to do otherwise.
  • /var/log/apache2/error.log: By default, all errors are recorded in this file. The LogLevel directive in the Apache configuration specifies how much detail the error logs will contain.

Wednesday, 7 February 2018

Change the root password for MySQL in XAMPP

By default, when you install XAMPP in your windows machine, the root password for the MySQL is set to empty. But this is not recommended, as the MySQL database without a password will be accessible to everyone. To avoid this, a proper/secure password must be set to the user root. To do it in XAMPP, there are two ways.

Method 1: reset XAMPP MySQL root password through web interface:

After you started your XAMPP server, go to the browser and type the URL http://localhost/security/ (incase you’ve modified XAMPP server port, you need to include that port number also in previous URL). The security page will be shown where you can change the root password for MySQL. This will update the phpMyAdmin config also.

Method 2: reset XAMPP MySQL root password through SQL update:

  1. Start the Apache Server and MySQL instances from the XAMPP control panel.
  2. After the server started, open any web browser and give http://localhost:8090/phpmyadmin/ (if you are running XAMPP on 8090 port). This will open the phpMyAdmin interface. Using this interface we can manager the MySQL server from the web browser.
  3. In the phpMyAdmin window, select SQL tab from the right panel. This will open the SQL tab where we can run the SQL queries.
  4. Now type the following query in the textarea and click Go
    UPDATE mysql.user SET Password=PASSWORD('password') WHERE User='root'; FLUSH PRIVILEGES;
  5. Now you will see a message saying that the query has been executed successfully.
  6. If you refresh the page, you will be getting a error message. This is because the phpMyAdmin configuration file is not aware of our newly set root passoword. To do this we have to modify the phpMyAdmin config file.
  7. Open the file [XAMPP Installation Path] / phpmyadmin / config.inc.php in your favorite text editor.
  8. Search for the string $cfg\['Servers'\]\[$i\]['password'] = ''; and change it to like this, $cfg\['Servers'\]\[$i\]['password'] = 'password'; Here the ‘password’ is what we set to the root user using the SQL query.
  9. Now all set to go. Save the config.inc.php file and restart the XAMPP server.

Thursday, 9 November 2017

Change Date Format From YYYY-MM-DD to DD-MM-YYYY also getting yesterday date for particular date in PHP. Retriving the day record from MySql

//////////////////////////////// GET LAST DATE OR YESTERDAYS /////////////////////////////////////////////////////////

$dt=date('d/m/Y');      // Gives todays date in dd/mm/yyyy format let it be my today's post publishing date i.e., 09/11/2017
 
$lastdate=0;               // Lets assusme lastdate is zero, as let x=0 
 
$vardate = str_replace('/', '-', $dt);   /* To convert the format we should first change our date as only digits, i.e., without special symbols  now vardate=09112017 */
 
$formatdt=date("Y-m-d",strtotime($vardate));  /* Now formatdt = 2017-11-09, dont miss strtotime function, missing it gives weird result such as 1970-01-01. */
 
$prevdt=DateTime::createFromFormat('Y-m-d',$formatdt);  /* Create a DateTime object, I tried several ways like date('d/m/Y',strtotime("-1 days"))  etc., but nothing worked well. This procedure delivered perfect result */
 
$prevdt->modify('-1 days'); 
/* Now we have current date in our object , we can do whatever we want like 
$start_date = '2013-03-06';
$date = DateTime::createFromFormat('Y-m-d',$start_date);

$date->modify('+1 month');
echo $date->format('Y-m-d');//2013-04-06

$date->modify('+4 year');
echo $date->format('Y-m-d');//2017-04-06

$date->modify('+6 day');
echo $date->format('Y-m-d');//2017-04-12

$date->modify('+24 hours');
echo $date->format('Y-m-d');//2017-04-13

$date->modify('-7 years');
echo $date->format('Y-m-d'); //2010-04-13

$date->modify('-18 months');
echo $date->format('Y-m-d'); //2008-10-13 
etc. 
 
Here our case is to get previous date so I used '-1 days' . Hope you understood */

$yesterdaydate=$prevdt->format('Y-m-d');  /* Obtaining previous date in yyyy-mm-dd format for database query retrieval purpose, Mysql date format is yyyy-mm-dd H:M:S */

$ydt=date('d/m/Y',strtotime($yesterdaydate));  // Converting previous date our reading format i.e., dd/mm/yyyy
 
$chkexists = mysql_result(mysql_query("SELECT IFNULL(MAX(id),0) FROM repdone WHERE tdate='$ydt'"),0);  /* Checking whether database entry exists in selected table on this particular date. This is case of daily reports. */
 
if($chkexists == 0){
  // CASE PREVIOUS DAY IS HOLIDAY FOR SELECTED DATE
 
  $chkdt=date('Y-m-d H:i:s',strtotime($yesterdaydate));
 /* GETTING previous date in mysql date format, note that we are using $yesterdaydate variable which is in yyyy-mm-dd format previous date, this format is best acceptable format for all coding languages but the users need readable format i.e., dd-mm-yyyy or dd/mm/yyyy format. That's why both formats are stored in separate variables as well as both formats are stored in database also. */

  $lastdate = mysql_result(mysql_query("SELECT IFNULL(MAX(repdate),0) FROM dailystatistics where repdate <= '$chkdt'"),0);
   // Getting Last maximum date  below the selected date from database through query
 
if($lastdate==0) {
  // In case database query fails. This code doesn't execute but for safe side it is written due to date format problems
 
  $chkrep = mysql_result(mysql_query("SELECT IFNULL(id,0) FROM repdone WHERE tdate='$dt'"),0); 
  /* If Todays  Record inserted then the value is 1 to get yesterdays date otherwise zero gives max id for yesterdays date */
  $lastrepid =  mysql_result(mysql_query("SELECT IFNULL(MAX(id),0) FROM repdone where id<$chkrep"),0);  // Getting just below id of selected date record
  if($lastrepid > 0) {
      $lastdate=mysql_result(mysql_query("SELECT IFNULL(tdate,0) FROM repdone where id='$lastrepid'"),0); // getting the date of the previous record id
  }
}

if($lastdate!=0)
  $vardate = str_replace('/', '-', $lastdate);  // Again for converting the date to dd/mm/yyyy format we need to replace the special symbols
  $ydt=date("d/m/Y",strtotime($vardate));  // Now we have previous date of selected date ($dt) in format dd/mm/yyyy is in $ydt variable
 
 } // End of chkexists


////////////////////////////////////////////////////////////////////////////////////////////////////////