William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

2 tips from work

PHP: header() can’t redirect

I added a login-check at the top of php:

session_start();
if( !isset($_SESSION['userid']) || empty($_SESSION['userid']) ) {
	header("Location: " . LOGIN);
	exit;
}

It didn’t work. Warning: Cannot modify header information – headers already sent by (output started at /home/william/test.php:5)

Since the codes use several class inheritances from different phps, so it is a little difficult to find where actually the problem is. Finally, I found the reason — very weird — it is caused by some spaces after the class definition :

class SmartyExtendBase extends Smarty {
  var $db1, $db2;
  function __construct() {
    parent::__construct();
    ...
  }
}spaces here......

So make sure the codes are clear and tidy: no more spaces in un-necessary place. A remedy checker in Linux is to use the following to display hidden/special chars:
$ cat -A test.php

MySQL: control 2 columns of timestamp type

When designing MySQL table, it is very common to have both created time and updated time cloumns: 1 for record created, 1 for record updated.

I use the following, works fine; it seems a good way to simplify both create and update operation:

create table test(...
  `createdby` varchar(50) DEFAULT NULL,
  `created` timestamp NOT NULL DEFAULT '2011-04-00 00:00:00',
  `updatedby` varchar(50) DEFAULT NULL,
  `updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
 );

According to MySQL timestamp, MySQL requires code doing the insert explicitly set a value for the created / inserted timestamp column, and no way to handle this implicitly.
So after the table created, we only care insert section: insert into test(…, created) values(…, now());
for update, no need to explicit, implicit works for ‘update’.

The following are some related helpful info.

1. For a single timestamp column, the create/update is simple and implicit:

  • Auto-initialization and auto-update:
    ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
  • Auto-initialization only:
    ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  • Auto-update only:
    ts TIMESTAMP DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP
  • Neither:
    ts TIMESTAMP DEFAULT 0

2. To specify automatic default or updating for a TIMESTAMP column other than the first one, you must suppress the automatic initialization and update behaviours for the first TIMESTAMP column by explicitly assigning it a constant DEFAULT value (for example, DEFAULT 0 or DEFAULT ‘2003-01-01 00:00:00’). Then, for the other TIMESTAMP column, the rules are the same as for the first TIMESTAMP column, except that if you omit both of the DEFAULT and ON UPDATE clauses, no automatic initialization or updating occurs. Like:

CREATE TABLE t (
  ts1 TIMESTAMP DEFAULT 0,
  ts2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    ON UPDATE CURRENT_TIMESTAMP);

3. The order of the DEFAULT and ON UPDATE attributes does not matter. If both DEFAULT and ON UPDATE are specified for a TIMESTAMP column, either can precede the other. For example, these statements are equivalent:

CREATE TABLE t (ts TIMESTAMP);
CREATE TABLE t (ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    ON UPDATE CURRENT_TIMESTAMP);
CREATE TABLE t (ts TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
   DEFAULT CURRENT_TIMESTAMP);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: