Wednesday, October 17, 2012

PHP session timeout cookie lifetime


There are many ways to set cookie based session timeout in PHP. You can edit your php.ini or put it in .htaccess but I choose for today this piece of code:

<?php
ini_set('session.gc_maxlifetime', 172800);
ini_set('session.cookie_lifetime', 172800);
session_start();
?>

The session above will be valid for 2 days.

Thursday, September 27, 2012

Redirect all pages to new domain


This code in .htaccess does redirect all requests to a new domain:

RewriteEngine On
RewriteRule ^(.*)$ http://WWW.NEW-DOMAIN.COM/ [R=301]

The problem was, all other directives work like: 

www.old-domain.com/bla -> www.new-domain.com/bla

but I need


www.old-domain.com/bla -> www.new-domain.com

www.old-domain.com/more/bla -> www.new-domain.com

So the htaccess code above works fine.




Friday, September 21, 2012

Kill Android AsyncTask if it hangs



I have an AsyncTask in my Android App that makes a network call. It may happen that the call for ever stuck. So I need a solution to handle this issue.


First I create a TimerTask that call cancel() to my AsyncTask:


class TaskKiller extends TimerTask {
private AsyncTask<?, ?, ?> mTask;
public TaskKiller(AsyncTask<?, ?, ?> task) {
this.mTask = task;
}

public void run() {
mTask.cancel(true);
}
}


Then I use this TimerTask with a timeout I need:


protected Boolean doInBackground(Object... params) {
Timer timer = new Timer();
timer.schedule(new TaskKiller(this), TIMEOUT);

// do some stuff, network, what ever can hangs
 // ...
// cancel the task if all things are done

timer.cancel();
}


onCancelled() will be called if your stuff needs longer than your timeout:

protected void onCancelled() {
// do something, inform user etc.
}

Thursday, September 20, 2012

htaccess non-www and www http to https


RewriteEngine on

RewriteBase /

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

<Files .htaccess>
order allow,deny
deny from all
</Files>

Wednesday, September 19, 2012

htaccess non-www to www


RewriteEngine on

RewriteBase /

RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

<Files .htaccess>
order allow,deny
deny from all
</Files>

Tuesday, September 18, 2012

Decrypt private key

I purchased today a Wildcard SSL certificate from RapidSSL. When I tried to insert it in Parallels Plesk, the private key was wrong. I found out that I must decrypt it first:

openssl rsa -in name_of_file.key -out decrypted.name_of_file.key