diff --git a/content/blog/2011-04-23-redirection.md b/content/blog/2011-04-23-redirection.md new file mode 100644 index 0000000..9f2f058 --- /dev/null +++ b/content/blog/2011-04-23-redirection.md @@ -0,0 +1,127 @@ +--- +title: "Serverside and Clientside Redirection" +date: 2011-04-23T00:01:30+05:30 +draft: false +author: rhnvrm +categories: + - notes +tags: + - php + - js +url: blog/2011/04/23/redirection/ +type: post +--- + +Redirection can be achieved Server-Side or Client-Side. + +Redirection on client side involves the following steps: + +1. User requests a Page using an URI. +2. Server sends a page based on that. +3. That page will have JavaScript that will redirect user to another page. +4. So, again server will get the request for the new page and then server will +give response based on new request. +5. That implies: +Client-side -> Server-side -> Client-side -> Server-side -> Client-side. + +Redirection on Server-Side: + +1. User requests a Page using URI. +2. Server sends a page based on that. +3. Client sends header information back to the server +(further reading: [HTTP Headers](http://www.softswot.com/http-header.php)) +4. The server responds very quickly +5. Client receives header and can follow quickly to the new location. +6. Client-side -> Server-side -> Client-side(header information) -> Server-side -> Client-side. + +Server-Side redirection is faster and more user friendly than Client-Side +redirection both for the client and the server. + +## PHP Redirection + +```php + +``` + +This piece of code redirects you to a different page, but the catch is — it +won’t work if you echo‘ed some output to the browser. The function actually +modifies the HTML headers to include location. Headers are always sent to the +browser before any content, and therefore it’s too late to modify them after +output has started. Fortunately, there is a quite +simple solution for that. + +### Output Buffers +further reading: [Output Control Functions](https://www.php.net/ref.outcontrol) +in PHP allow you to collect all output data into an output buffer before sending + it to the browser. Using the following code: + +```php + +``` + +## Javascript Redirection + +Now we will see redirection using Javascript that is supported by every +modern browser and does not require any resources of the server. Javascript is +an object oriented language and blends with HTML. + +So, you want to move to another webpage after a certain interval. Or you want +to tell your visitors that your site has moved and transfer the to your +new site. Or you want to make an advertisement service. Well then Javascript +redirection is for you. + +The primary use of JavaScript is to write functions that are embedded in +or included from HTML pages and that interact with the [Document Object +Model](http://en.wikipedia.org/wiki/Document_Object_Model) (DOM) of the page. + +So, what we can logically do is to alter the window’s location property +using the following code: + +```html + +``` + +Now you might want to create a timed delay between redirection so that +people know that they are being transfered to a new location. + +```html + + + + + +

Prepare to be redirected!

+

+ This page is a time delay redirect, + please wait for 5000 milliseconds (5 seconds) +

+ + +``` + +[setTimeout()](https://www.w3schools.com/jsref/met_win_settimeout.asp) Method +calls a function or evaluates an expression after a specified number of +milliseconds, where: + +```javascript +setTimeout(myfunc(),millisec) +``` + +millisec = the number of milliseconds to wait before executing the code \ No newline at end of file