Author Archives: invtr

iPhone CSS

This CSS to be used to display website nicely on iPhone. Not tested on Android and other smart phones.

Add this code on top of what ever css style you have.

1
2
3
4
5
<!-- Start iPhone -->
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
<link rel="apple-touch-icon" href="/images/money.png" />
<link media="only screen and (max-device-width: 480px)" href="/iphone.css" type= "text/css" rel="stylesheet" />
<!-- End iPhone -->

In iphone.css file, just write the css code. For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
body,td,input,textarea,select,label { font-size: 6em;}
mainlink {font-size:3em; }
body {
background-color: #fff;
}
 
.header, .footer {
width: 100%;
}
 
.sidebar {
display: none;
}
 
.mainlink {	color:#FFFFFF; font-size:150%; }
 
.mainlink A:link {text-decoration: none; color:#FFFFFF; }
 
.mainlink A:visited {text-decoration: none; color:#FFFFFF; }
 
.mainlink A:active {text-decoration: none; color:#FFFFFF; }
 
.mainlink A:hover {text-decoration: underline; color:#FFFF66; }

Reset root password for MySQL on windows

If you have problem accessing your MySQL database with this error
ERROR 1045: Access denied for user: ‘root@localhost’ (Using password: NO)

You can try reset your root password with the following steps:

1. Create a txt file with this content

UPDATE mysql.user SET Password=PASSWORD('newpassword') WHERE User='root';
FLUSH PRIVILEGES;

2. Save the file as mysql-init.txt in c:\ (or any directory you want)

3. Stop MySQL service

4. Start MySQL service in command prompt with this command line

>c:\mysql\bin\mysqld --init-file=c:\\mysql-init.txt

5. Remove the txt file in created in step 2

Note: You can/need to specify correct path for the file and mysql directory for step 2 and 4.

MySQL database auto backup on windows

To backup mysql database (all databases) can use this batch file

Then you can compile the .bat file to become .exe file so that the password is hidden.

Download bat to exe converter from CNET download

Lastly, you can include the exe file to run in task scheduler

You can have option to upload the file via FTP to other server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
:: Start FTP files to remote server
:: Create the temporary FTP script file
> script.ftp ECHO ftpusername
>>script.ftp ECHO ftppassword
>>script.ftp ECHO lcd /
>>script.ftp ECHO lcd /MySQLBackups/backupfiles
>>script.ftp ECHO binary
>>script.ftp ECHO prompt n
>>script.ftp ECHO put FullBackup.%backupdate%.zip
>>script.ftp ECHO bye
:: Use the temporary script for unattended FTP
:: Note: depending on your OS version you may have to add a '-n' switch
FTP -v -s:script.ftp ftphostcom.com
:: Overwrite the temporary file before deleting it
TYPE NUL >script.ftp
DEL script.ftp
:: DONE FTP backup file to remote server

Database config for localhost and server

To easily toggle database config for localhost and server, you can simply check whether you are accessing the local or server.

1
2
3
4
5
6
7
8
9
10
11
12
13
if (preg_match("/localhost/i", $_SERVER['SERVER_NAME'])) {
define ('DBHOST','localhost');
define ('DBNAME','localdbname'); 
define ('DBUSER','localuser');
define ('DBPASS','localpass');
define ('DEV',TRUE);
} else {
define ('DBHOST','localhost');
define ('DBNAME','serverdbname'); 
define ('DBUSER','serveruser');
define ('DBPASS','serverpass');
define ('DEV',FALSE);
}

Inserting recaptcha to your form

Captcha is good to reduce spam. One of most popular captcha used is recaptcha.

This is how it looks like. Familiar?

captcha

To implement it is very easy. Will take less than 10 minutes of your time.

1. Get your public and private key

https://www.google.com/recaptcha/admin/create

2. Include class file

Download recaptcha library file for PHP (with some sample files)

3. Insert code in form page

1
2
3
4
5
6
7
8
<form method="post" action="verify.php">
        <?php
          require_once('recaptchalib.php');
          $publickey = "your_public_key"; // you got this from the signup page
          echo recaptcha_get_html($publickey);
        ?>
        <input type="submit" />
</form>

4. Insert code to validate code in process page

1
2
3
4
5
6
7
8
9
10
11
12
13
14
require_once('recaptchalib.php');
  $privatekey = "your_private_key";
  $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);
 
  if (!$resp->is_valid) {
    // What happens when the CAPTCHA was entered incorrectly
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
         "(reCAPTCHA said: " . $resp->error . ")");
  } else {
    // Your code here to handle a successful verification
  }

Resume do’s and don’ts

If you are sending out resume to apply for job, please follow the following rules

  • Prepare and send your resume in PDF format. With this your resume formatting will appear same all the time
  • Please always include your LATEST and most PRESENTABLE photo
  • Don’t name your file as resume.doc. Use resume_yourname.pdf instead. Remember not only you that send resume to apply for the job
  • Use email with your real name, not your cat’s name or whatever crazy name. For example abdul.rahman@gmail.com NOT setan_cute@gmail.com
  • Tell about your experience and what you HAVE DONE. Not about what subjects you took during your study. Describe the task, your role and other related information (e.g. technology used, party involved etc)
  • There is no limit of page for a resume. Don’t buy to idea that you need to limit to 1 page, 2 pages or maximum 4 pages. If you write the right thing, people will read it from beginning to the end

Update with join table

To update a table with a condition within other table (to join table), you can use this statement

1
2
3
UPDATE table1 a, table2 b
SET a.field6 = value
WHERE a.field1 = b.field1 AND b.field2 = value2

MySQL insert into select

This SQL statement to be used to insert data into a table from another different table.

1
2
3
INSERT INTO table1 (field1)
  SELECT table2.field2 FROM table2 
  WHERE table2.field2 > 100;

Be careful with ambiguous fields. So put the table name in front of the field name.
Also be careful with duplicate data inserted to the table.

Update with select statement

1
2
3
4
UPDATE student_transaction 
SET studentid = 
(SELECT newstudentid FROM student_newid 
WHERE studentid = student_transaction.studentid)

PHP code protection

I’m looking for PHP encoding application to encrypt some of my PHP code. Reasons – to avoid it from being accidently altered and to avoid from people looking into the code (especially those with some important db and system config in it)

Here are some solutions for PHP encoding

  1. PHP Obfuscator – free (native windows, open source)
  2. zend guard
  3. ioncube (usd199)
  4. SourceGuardian (usd199)
  5. PHPshadow (EUR50 per domain)
  6. more..

Some other related discussion on license/code protection:

  1. stackoverflow
  2. webmasterworld
  3. PADL (a php class) – PHP Application Distribution License System