Friday, January 31, 2014

PHP - Export Content to MS Word document



The following PHP code to export content in MS Word document.
 <?php
 $doc_body ="
 <h1>PHP - Export Content to MS Word document</h1>
 <p>This is a test.<p>
 <p>This is a test.<p>
 ";
 ?>
 <form name="proposal_form" action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post"">
  <input type="submit" name="submit_docs" value="Export as MS Word" class="input-button" />
</form>
<?php
  if(isset($_POST['submit_docs'])){
          header("Content-Type: application/vnd.msword");
          header("Expires: 0");//no-cache
          header("Cache-Control: must-revalidate, post-check=0, pre-check=0");//no-cache
          header("content-disposition: attachment;filename=sampleword.doc");
  }         
          echo "<html>";
          echo "$doc_body";
          echo "</html>";      
?>


Thursday, January 30, 2014

screenr not working even after Java is updated



I used to use screenr to do screen capture:
http://www.screenr.com/
Recently  I found screenr record not working  even  after Java is updated. The error message:  "No Java detected".   Later I found it is caused by Java security setting.
1)  On Windows Click Start and then in search box type:
Configure Java
 2) Java Control Panel pop up
3) Click on the Security tab
4) Move Security level from high to medium.
5) Under "Exception site list", click "Edit site  list", add www.screenr.com
Video:  Solved "No Java Detected" Error in Screenr screen record in Windows 7 

Reference:
http://www.java.com/en/download/help/java_blocked.xml

Tuesday, January 28, 2014

PHP and JavaScript, redirect url with alert message or confirm








In PHP, when we use header to redirect to a new url, we may find the alert message disappear. The following code will not show alert message:
<script>
          alert("User csv data is imported into publications database");

 </script>
<?php
header("Location: index.php");
?>

To solve this,
method 1:
we can use:
<?php
$message =  "User csv data is imported into publications database";
header("Location: index.php?message=" . urlencode($message));
?>
in index.php, add
if (isset($_GET['message'])) { echo  $_GET['message']; }




method 2: only using JavaScript
<script>
          alert("User csv data is imported into publications database");
          window.location.href ="../index.php" 

</script>

method 3: JavaScript with confirm yes or no
<script>
         if(confirm('Are you sure you want to redirect homepage?')) {location.href='index.php'};
   </script>


 Another Javscript redirection example, redirect to different websites via choose the option list
<select onchange="if(this.value!='') location.href='index.php?userid='+this.value">
          <option value=""> --- Choose a person --- </option>

           <option value="1"> Jiansen </option> 
            <option value="2"> Tom </option>  
            <option value="3"> Jerry </option>           
     </select>  

PHP - divide strings into array or words



In PHP, we can use explode to divide strings into array or words.
For example:
< ?php
                $namestring = "Jiansen Lu";
                $namearray  =  explode(" ", trim($namestring));
                var_dump($namearray);

?>
Result:
array(2) { [0]=> string(7) "Jiansen" [1]=> string(2) "Lu" } 

But this may cause problem when there are two or more spaces between words, for example:

< ?php
                $namestring = "Jiansen  Lu";//Two spaces between words
                $namearray  =  explode(" ", trim($namestring));
                var_dump($namearray);

?>
Result:
 array(3) { [0]=> string(7) "Jiansen" [1]=> string(0) "" [2]=> string(2) "Lu" }
The second element, i.e. [1] is no more Lu, but is an empty string.

To resolve problem for two or more space in explode function, it is better to  use regular expression.
Example
< ?php
                $namestring = "Jiansen  Lu";//Two spaces between words
               
$namearray = preg_split('/\s+/',trim($namestring));
                var_dump($namearray);

?>
Result:
 array(2) { [0]=> string(7) "Jiansen" [1]=> string(2) "Lu" }

Friday, January 24, 2014

MySQL query -find match strings using like, FIND_IN_SET and REGEXP



I have a publication database. authors are  separated as comma in id i,.e authoridlist column is
1,2,10,20. Using like in the query will cause the matching strings inaccuracy.
For example:
select * from publications where authoridlist like '%1%';
will find the author id is not only 1, but also 10 as % in MySQl query means  any number of characters.

To exactly find the substring  or number in  comma separated strings, we can use FIND_IN_SET:
select * from publications where FIND_IN_SET('1',authoridlist);
 or using REGEXP:
select * from publications where authoridlist REGEXP '[[:<:]]1[[:>:]]';
REGEXP: regular expression
[[:<:]] match beginning of  the word
 [[:>:]] match end of  the word
 [[:<:]]1 [[:>:]]  match a word boundary at the beginning and  end of a word, here the word i s 1.

Tuesday, January 21, 2014

Apache - disable and enable directory listing



Sometimes you may find your web directory is listed when there is no index.php and index.html.
You can disable directory listing in Apache configuration.
For example, your web root directory is: "C:/LMS/canhealth"
To disable directory listing, in httpd.conf, add Options -Indexes
<Directory "C:/LMS/canhealth">
    Options -Indexes
</Directory>
You can see a message when accessing this directory if there is no index.php and index.html :

Forbidden

You don't have permission to access /canhealth/ on this server.

To enable   directory listing,
in httpd.conf
<Directory "C:/LMS/canhealth">
    Options +Indexes
</Directory>
Video: Apache - disable and enable directory listing



Monday, January 20, 2014

A tool to test if a port is blocked by firewall



To test if an  Internet socket port on an external server is open or blocked by a firewall, input the ip address and port in this address
http://www.alicka.com/tools.html
you'll get if the port is blocked or open via firewall.

Although the port is open in firewall,  if it is not actively used, it will still show closed.

To test gmail ssl port 465, download openssl Win32 OpenSSL v1.0.1f Light from:
http://slproweb.com/products/Win32OpenSSL.html
Run the program and assume you installed it in C:\OpenSSL-Win32:
run
C:\OpenSSL-Win32\bin\openssl  s_client -connect smtp.gmail.com:465
to see if port 465 open.

To if test gmail port 25 open in firewall: in command prompt run
nslookup -q=MX gmail.com
You can get
Server:  ns-int-1.netcom.ubc.ca
Address:  137.82.1.2

Non-authoritative answer:
gmail.com       MX preference = 40, mail exchanger = alt4.gmail-smtp-in.l.google
.com
gmail.com       MX preference = 5, mail exchanger = gmail-smtp-in.l.google.com
gmail.com       MX preference = 10, mail exchanger = alt1.gmail-smtp-in.l.google
.com
gmail.com       MX preference = 20, mail exchanger = alt2.gmail-smtp-in.l.google
.com
gmail.com       MX preference = 30, mail exchanger = alt3.gmail-smtp-in.l.google
.com

gmail.com       nameserver = ns1.google.com
gmail.com       nameserver = ns3.google.com
gmail.com       nameserver = ns4.google.com
gmail.com       nameserver = ns2.google.com
gmail-smtp-in.l.google.com      internet address = 74.125.129.26
gmail-smtp-in.l.google.com      AAAA IPv6 address = 2607:f8b0:4002:c01::1a
alt1.gmail-smtp-in.l.google.com internet address = 74.125.193.26
alt1.gmail-smtp-in.l.google.com AAAA IPv6 address = 2607:f8b0:4002:c01::1a
alt2.gmail-smtp-in.l.google.com internet address = 74.125.196.27
alt2.gmail-smtp-in.l.google.com AAAA IPv6 address = 2607:f8b0:4002:c01::1b
alt3.gmail-smtp-in.l.google.com internet address = 173.194.68.26
alt3.gmail-smtp-in.l.google.com AAAA IPv6 address = 2607:f8b0:4002:c01::1b
alt4.gmail-smtp-in.l.google.com internet address = 173.194.75.27
alt4.gmail-smtp-in.l.google.com AAAA IPv6 address = 2607:f8b0:4002:c01::1a
ns1.google.com  internet address = 216.239.32.10
ns2.google.com  internet address = 216.239.34.10
ns3.google.com  internet address = 216.239.36.10
ns4.google.com  internet address = 216.239.38.10

Pick up one mail exchanger for example alt3.gmail-smtp-in.l.google.com, run
telnet alt3.gmail-smtp-in.l.google.com 25
if successful, you can get
221 2.0.0 closing connection w8si3911414qag.86 - gsmtp
type quit to quit telnet.
Do not too trust your host provider. Test it. Some important ports: port 25 SMTP (Simple Mail Transfer Protocol) and 465 Secure SMTP (often used by email providers such as Google-Apps for outbound email traffic), ftp (port 21), sftp (port 22), Web service (ports 80, 8080), MySQL (port 3306), Flash Media server (port 1935).

In PHPMailer, if we use ssl in smtp.gmail.com, port 465 outbound in Firewall should be open.
If we use other regular   outside smtp mail server, port 25 outbound should be open.

Inbound rules allow other system to connect to yours, ex if you would like someone to connect to your windows shares, ftp, web server etc.
Outbound rules allow applications on you system to connect to other systems, ex if you want to connect to a web site, IM or some elses ftp.
Both the inbound and outbound rules will have no effect if your firewall is off.

Thursday, January 9, 2014

Search a word or a string in MySQL



We often perform searching a word or a string in MySQL. For example, we want to search Bowen from column authorlist, secondaryauthorlist and seriesauthorlist in table publications, we can use
SELECT * FROM publications WHERE  (CONCAT_WS(' ', authorlist, secondaryauthorlist, seriesauthorlist) LIKE '%Bowen%';
or
SELECT * FROM publications WHERE CONCAT_WS(' ', authorlist, secondaryauthorlist, seriesauthorlist) REGEXP '[[:<:]]Bowen[[:>:]]';

There is a small difference between these two queries. The first one matches Bowen, CBowen, CBowenm ..., the second one matches only Bowen, more explanation:

 MySQL function CONCAT_WS joins strings with separator.
CONCAT_WS(separator, string1, string2)
LIKE '%Bowen%': find string containing Bowen, which can be in the middle of a string
REGEXP: regular expression
[[:<:]] match beginning of  the word
 [[:>:]] match end of  the word
 [[:<:]]Bowen [[:>:]]  match a word boundary at the beginning and  end of a word,
i.e. BBowenn will not match, while LIKE '%Bowen%' match

Tuesday, January 7, 2014

Create ftp and Sftp server account in Windows Server 2003 using built-in IIS service



Using  Windows Server 2003  built-in IIS service, we can create ftp and sftp server account without using external ftp server software such as FileZilla server.

A) Below  is to create  ftp account  via IIS manager in Windows 2003

1. Create FTP User
 Start -> Administrator Tools->Computer Management->Local Users and Groups->Users
or in active directory
Start -> Administrator Tools->Active Directory Users and computer->Users->New User (right click mouse)

2.  Create FTP Virtual Directory

    Start -> Administrator Tools->Internet Information Service (IIS) Manager
  ->FTP Sites > new virtual directory (right click  mouse)
  Enter "Alias" name, put it as exactly the same as the username you have just created and click Next, Browse to the directory where you wish to allow this FTP user to access to. Click Next, enable "Read" and "Write" privileges. Click Finish


3 .Create Permissions on FTP Folder

 Right click on your FTP folder and add the FTP user you have just created above to this folder.
Enable full permissions or any permission set you wish to create.

B)  Below  is to create  sftp account  via IIS manager in Windows 2003

  Step 1) 2) 3) is the same as above in creating ftp account. for example I already created ftp account user name jiansen
Step 4) Download OpenSSH:
http://www.openssh.com/
for example in C:\Program Files\OpenSSH\
cd C:\Program Files\OpenSSH\bin
mkpasswd -l -u jiansen >> ..\etc\passwd
  (change jiansen to your ftp user name)
edit ..\etc\passwd, change /home/jiansen (change jiansen to your ftp user name) to your ftp directory such as /cygdrive/c/web_root
restart OpenSSH
Video:Create ftp and Sftp server account in Windows Server 2003 using built-in IIS service

Reference:
1. http://support.microsoft.com/kb/323384
2. https://www.serverintellect.com/support/ftp/manual-ftp-acct/
3. http://www.digitalmediaminute.com/article/1487/setting-up-a-sftp-server-on-windows



Saturday, January 4, 2014

Choose Flash Media Server




There are Flash Media Development Server, Flash Media Streaming Server, Flash Media Interactive Server, Flash Media Enterprise Server, Flash Media Server on Amazon Web Services. The difference can be found:
http://www.adobe.com/products/flashmediaserver/helpmechoose.html

The Flash Media Development Server is a free version of Flash Media Enterprise Server (around 45,000$) with limitation of  10 RTMP and 10 RTMFP.

Flash Media Development Server 4.5 can be downloaded 
https://www.adobe.com/cfusion/tdrc/index.cfm?loc=en_us&product=flashmediaserver

During installation, you do not need to input production key.


Flash Media Streaming Server (around $995) is not with RTMP.

In small to media websites which  need RTMP and recording, we  can choose  Flash Media Interactive Server (around $4500), which has feature: 500 RTMFP,  real-time communication and protect/record  media.


Only  Flash Media Server4.5 and later support IOS device streaming, which uses HTTP live streaming (HLS).