Cron is the Linux equivalent of Window's Task Scheduler.
Jobs are executed from /etc/crontab
1. Create a PHP script file.
2. Edit the crontab
# crontab –e
This is the file syntax:
minutes hours day_of_month month day_of_week command
where:
minutes: 0-59
hours: 0-23
day_of_month: 1-31
month: 1-12
weekday: 0-6
command: Any shell command or web document
Wildcard: * (Allow any value)
I will use wget because PHP files need to be parsed by Apache, we need to execute it via wget command using the URL to the page. To check if your box has wget:
# wget --help
To run the script every Wednesday morning at 9:30 AM, the cronjob file will contain the following in single line:
30 9 * * 3 wget http://www.domain.com/file.php
Save the file and add to crontab
# crontab samplecronjob
Monday, July 10, 2006
PHP: Half-byte Katakana conversion (Single-byte, Hankaku, 半角カタカナ)
mb_convert_kana() is not working for me so I found a neat php code that can convert Hankaku to Zenkaku katakana and vice versa. Also has character set conversion. It's called JCode. It's free without warranty. I've only tested jcode-LE for now.
This is the link for the one with UTF-8:
http://www.spencernetwork.org/jcode/
This is the light edition without UTF-8, only supports EUC-JP, Shift-JIS, ISO-2022-JP (JIS):
http://www.spencernetwork.org/jcode-LE/
This is the link for the one with UTF-8:
http://www.spencernetwork.org/jcode/
This is the light edition without UTF-8, only supports EUC-JP, Shift-JIS, ISO-2022-JP (JIS):
http://www.spencernetwork.org/jcode-LE/
Browsers: Compatibility
This will work in some browsers like IE. elementName is from form object's name.
formName.elementName
For compatibility, use form object's id
document.getElementById('elementID')
Another issue is using comments
<!-- comment -->
This is okay in MS IE but not in Mozilla Firefox. So avoid using this style of comment. Use only two hyphens/dashes.
<!------------ comment ----------->
(Thanks to Cacho for this)
Manipulating style attribute's display using javascript, use '' instead of 'block' or other values aside from 'none' to go back to it's default value. This is useful when showing/hiding object. Other common values are 'table-row' and 'inline'.
object.style.display = 'none';
object.style.display = ''; // restores default value.
IME Mode (Internationalization format) is an IE specific attribute in CSS.
formName.elementName
For compatibility, use form object's id
document.getElementById('elementID')
Another issue is using comments
<!-- comment -->
This is okay in MS IE but not in Mozilla Firefox. So avoid using this style of comment. Use only two hyphens/dashes.
<!------------ comment ----------->
(Thanks to Cacho for this)
Manipulating style attribute's display using javascript, use '' instead of 'block' or other values aside from 'none' to go back to it's default value. This is useful when showing/hiding object. Other common values are 'table-row' and 'inline'.
object.style.display = 'none';
object.style.display = ''; // restores default value.
IME Mode (Internationalization format) is an IE specific attribute in CSS.
Wednesday, June 28, 2006
Linux: Setting the Hostname
This is useful when your linux box is used for sending emails. If hostname is incorrect, it will be detected by http://cbl.abuseat.org and will be blocked from sending emails.
Example of a computer's hostname: mybox.mydomain.com
Replace example hostname to your computer's hostname.
In Linux, to check the hostname if it is similar to this example:
[root@mybox ~]# uname -n
mybox.mydomain.com
[root@mybox ~]# hostname -s
mybox
[root@mybox ~]# hostname -d
mydomain.com
[root@mybox ~]# hostname -f
mybox.mydomain.com
[root@mybox ~]# hostname
mybox.mydomain.com
If it returns localhost.localdomain then it's wrong. If not then your linux box is ok. (Very big hint for me was that before, the command prompt was [root@localhost ~]#)
For a static IP Address, /etc/hosts is configured as follows:
127.0.0.1 localhost.localdomain localhost
192.168.0.xxx mybox.mydomain.com mybox
Change 192.168.0.xxx to your static IP Address.
Set hostname:
[root@localhost ~]# hostname mybox.mydomain.com
Check /etc/sysconfig/network file. Must have be like this:
NETWORKING=yes
HOSTNAME="mybox.mydomain.com"
For further reference:
http://www.cpqlinux.com/hostname.html
* Using Fedora Core 3
Example of a computer's hostname: mybox.mydomain.com
Replace example hostname to your computer's hostname.
In Linux, to check the hostname if it is similar to this example:
[root@mybox ~]# uname -n
mybox.mydomain.com
[root@mybox ~]# hostname -s
mybox
[root@mybox ~]# hostname -d
mydomain.com
[root@mybox ~]# hostname -f
mybox.mydomain.com
[root@mybox ~]# hostname
mybox.mydomain.com
If it returns localhost.localdomain then it's wrong. If not then your linux box is ok. (Very big hint for me was that before, the command prompt was [root@localhost ~]#)
For a static IP Address, /etc/hosts is configured as follows:
127.0.0.1 localhost.localdomain localhost
192.168.0.xxx mybox.mydomain.com mybox
Change 192.168.0.xxx to your static IP Address.
Set hostname:
[root@localhost ~]# hostname mybox.mydomain.com
Check /etc/sysconfig/network file. Must have be like this:
NETWORKING=yes
HOSTNAME="mybox.mydomain.com"
For further reference:
http://www.cpqlinux.com/hostname.html
* Using Fedora Core 3
Tuesday, June 27, 2006
Forms: Input validation
Input text and text areas
- For required fields, is string empty? Remove trailing spaces.
- Scope of string. (numeric, letters only ??)
- Minimum and maximum length of string.
Input file
- Is string empty?
- Does file exist?
- File must be of valid type.
- Limit the type of files that can be uploaded.
Check box
- Grouped check boxes must be same name.
- If multiple check boxes, can check multiple boxes.
- If field is required, at least one check box checked or depending on specs.
Radio button
- Grouped radio buttons must be same name.
- If multiple radio buttons, can only check one radio button per group.
- If field is required, must have ticked a radio button.
Selections (Dropdown)
- Do not put default empty value if required field.
- Check other dropdown in same group if not selected. (like in date and time)
- For group selections, check format or combination.
Input submit (Submit button)
- Do not submit if there are invalid input in form.
Input reset (Reset button)
- All default values must be returned as if the form was loaded again from the start.
Thursday, June 22, 2006
Linux: Accept Local SSH only
Linux Firewall must be running.
In /etc/sysconfig/iptables
find the following line
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
and change to
-A RH-Firewall-1-INPUT -s xxx.xxx.xxx.0/24 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
replace xxx.xxx.xxx.0 with local IP range.
restart firewall
[root@localhost ~]# service iptables restart
(Thanks to Sasi)Thursday, May 04, 2006
Linux: Sendmail
Linux must have direct internet access.
Sendmail is located in /etc/mail
The editable configuration file is sendmail.mc.
The default settings are ok if you don't like to edit the file.
After editing, type the following command:
[root@localhost ~]# make -C /etc/mail
If sendmail-cf is not installed this will generate an error. So install sendmail-cf.
[root@localhost ~]# yum install sendmail-cf
After installation of sendmail-cf, make again. When OK, restart sendmail.
[root@localhost ~]# service sendmail restart
* Using Fedora Core 3
Sendmail is located in /etc/mail
The editable configuration file is sendmail.mc.
The default settings are ok if you don't like to edit the file.
After editing, type the following command:
[root@localhost ~]# make -C /etc/mail
If sendmail-cf is not installed this will generate an error. So install sendmail-cf.
[root@localhost ~]# yum install sendmail-cf
After installation of sendmail-cf, make again. When OK, restart sendmail.
[root@localhost ~]# service sendmail restart
* Using Fedora Core 3
Tuesday, May 02, 2006
MySQL: Creating views and functions
Make sure thet the database user has SUPER Privilege, CREATE VIEWS and CREATE ROUTINE. In SQL Query type the following:
mysql> SET GLOBAL log_bin_trust_routine_creators = 1;
* Using MySQL 5.0.18
mysql> SET GLOBAL log_bin_trust_routine_creators = 1;
* Using MySQL 5.0.18
PHP: Uploading SJIS files to EUC-JP charset
This is assuming that mb_string is installed. Use iconv for Windows
In php.ini:
[mbstring]
; language for internal character representation.
mbstring.language = Japanese
; internal/script encoding.
; Some encoding cannot work as internal encoding.
; (e.g. SJIS, BIG5, ISO-2022-*)
mbstring.internal_encoding = EUC-JP
; http input encoding.
mbstring.http_input = auto
; http output encoding. mb_output_handler must be
; registered as output buffer to function
mbstring.http_output = EUC-JP
; enable automatic encoding translation accoding to
; mbstring.internal_encoding setting. Input chars are
; converted to internal encoding by setting this to On.
; Note: Do _not_ use automatic encoding translation for
; portable libs/applications.
mbstring.encoding_translation = On
; automatic encoding detection order.
; auto means
;mbstring.detect_order = auto
; substitute_character used when character cannot be converted
; one from another
;mbstring.substitute_character = none;
; overload(replace) single byte functions by mbstring functions.
; mail(), ereg(), etc are overloaded by mb_send_mail(), mb_ereg(),
; etc. Possible values are 0,1,2,4 or combination of them.
; For example, 7 for overload everything.
; 0: No overload
; 1: Overload mail() function
; 2: Overload str*() functions
; 4: Overload ereg*() functions
;mbstring.func_overload = 0
In upload.php:
// Set encoding of iconv
iconv_set_encoding('internal_encoding', 'EUC-JP');
iconv_set_encoding('input_encoding', 'SJIS');
iconv_set_encoding('output_encoding', 'EUC-JP');
// Set linux local to japanese
setlocale(LC_ALL, 'ja_JP.EUC-JP');
// Read data from file one row at a time
while (($data = fgets($handle)) !== false) {
// Convert SJIS (or other charset) to EUC-JP
$dataWindows = trim(iconv(mb_detect_encoding($data), _CHARSET.'//TRANSLIT', $data));
$dataLinux = trim(mb_convert_encoding($data, 'EUC-JP', mb_detect_encoding($data)));
}
* Using PHP 5.0.3
In php.ini:
[mbstring]
; language for internal character representation.
mbstring.language = Japanese
; internal/script encoding.
; Some encoding cannot work as internal encoding.
; (e.g. SJIS, BIG5, ISO-2022-*)
mbstring.internal_encoding = EUC-JP
; http input encoding.
mbstring.http_input = auto
; http output encoding. mb_output_handler must be
; registered as output buffer to function
mbstring.http_output = EUC-JP
; enable automatic encoding translation accoding to
; mbstring.internal_encoding setting. Input chars are
; converted to internal encoding by setting this to On.
; Note: Do _not_ use automatic encoding translation for
; portable libs/applications.
mbstring.encoding_translation = On
; automatic encoding detection order.
; auto means
;mbstring.detect_order = auto
; substitute_character used when character cannot be converted
; one from another
;mbstring.substitute_character = none;
; overload(replace) single byte functions by mbstring functions.
; mail(), ereg(), etc are overloaded by mb_send_mail(), mb_ereg(),
; etc. Possible values are 0,1,2,4 or combination of them.
; For example, 7 for overload everything.
; 0: No overload
; 1: Overload mail() function
; 2: Overload str*() functions
; 4: Overload ereg*() functions
;mbstring.func_overload = 0
In upload.php:
// Set encoding of iconv
iconv_set_encoding('internal_encoding', 'EUC-JP');
iconv_set_encoding('input_encoding', 'SJIS');
iconv_set_encoding('output_encoding', 'EUC-JP');
// Set linux local to japanese
setlocale(LC_ALL, 'ja_JP.EUC-JP');
// Read data from file one row at a time
while (($data = fgets($handle)) !== false) {
// Convert SJIS (or other charset) to EUC-JP
$dataWindows = trim(iconv(mb_detect_encoding($data), _CHARSET.'//TRANSLIT', $data));
$dataLinux = trim(mb_convert_encoding($data, 'EUC-JP', mb_detect_encoding($data)));
}
* Using PHP 5.0.3
Subscribe to:
Comments (Atom)