Web Hosting Deals Holiday Logo Design Webcam Chat Website Header Templates Register domain Search Engine Optimisation Web Hosting
Go Back   Talk Mania Forum > Tutorials > PHP / Perl / Java / JavaScript / CGI Tutorials

PHP / Perl / Java / JavaScript / CGI Tutorials PHP / Perl / Java / JavaScript / CGI Tutorials Please do not use this Forum to advertise your site or to link to tutorials.

 Image
Buy Sell Downloads

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-12-2007, 01:54 PM
Junior Member
 
Join Date: Apr 2007
Posts: 3
zeon is on a distinguished road
Redirect with php

-could admin move to the correct section, seems i posted in the wrong one?..-

well, there are a few ways to do it.
often you will use a header for it. but what if you want to redirect people after a script is done?. well i had this problem, and finaly found a solution.

we will first need a header in the top of your page.
PHP Code:
<?
header
('Location: http://www.yourpage.com/');
?>
What this will do, is that it will send people to that page. but what if we want it later, after the script?.
this is simple, simple set
PHP Code:
<?
ob_start
();
?>
instead, then move the header down to the end of that script you have.
now we have the header, but it doesn't run yet. what we need to do is to use
PHP Code:
<?
ob_end_flush
();
?>
this code can be placed at the bottom on your page.

now it will redirect you instantly when the script is run.

a test can be seen in action Here

full code for that page below.
PHP Code:
<?php
ob_start
();
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Post something</title>
</head>

<body>
<?php
// here my code starts.
if(isset($_POST['Submit'])) {
// all your code, could be entering a form to database or something...

header('Location: http://www.shexna.dk/');
// if Ends
?>
<?
ob_end_flush
();
?> 
<form id="form1" name="form1" method="post" action="">
  <input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
hope this helps. ask me if you have any questions, or suggest changes to it.


Last edited by zeon; 12-12-2007 at 03:33 PM.
Reply With Quote
  #2 (permalink)  
Old 12-29-2007, 06:13 AM
Member
 
Join Date: Oct 2006
Posts: 47
weblizzer is on a distinguished road
Smile

Very nice tutorial... but i would like to ask

what if there are many headers in redirection to code let say

you have

header("location: index.php");

and then in the middle part you have also condition to redirect to different page

header("location: redirectanotherpage.php");


so the how will i put the ob_flush(); function?
shall i use it afterwards everytime i put a header location?

for redirection?
Reply With Quote
  #3 (permalink)  
Old 12-30-2007, 02:22 AM
Junior Member
 
Join Date: Nov 2007
Posts: 1
katmai is on a distinguished road
@weblizzer
You need only one time,ob_start at the begging and ob_end_flush(); at end,ok so if u have many headers it's not really a problem because you need to execute if a condition is true or false.

if($_POST['a'] == 'How you doing'){
header("Location: http://blah.com");
}else{
header ("Location : http://blah1.com");
}

or you can have more conditions

<?php
if($_POST['a'] == 'Mother'){
header("Location: http://blah.com");
}elseif ($_POST['a'] == 'Father'){
header ("Location : http://blah1.com");
}elseif ($_POST['a'] == 'Sister'){
header ("Location : http://blah2.com");
}elseif ($_POST['a'] == 'Tony'){
header ("Location : http://blah3.com");
}
?>

Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT +2. The time now is 12:36 AM.


Fake ID

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC8
Forums Copyright © Talk-Mania.com