Hi,
I am a novice programmer at PHP. I want to redirect my webpage to another webpage. The problem is that First I want to output a file as an attachment and then redirect the web page
I have used this code to output the attachment file

header("Content-Disposition: attachment; filename=$filename");
echo $out;

where

$out

is the text I want to write in my attachment file.
Now Since I have used echo here I cant use

header("location:");

to redirect my webpage. I have also tried to use ob_start() and ob_end_flush() but then my file wont be outputted as an attachment. What should I do?

You can either start a download, or redirect. Both is not possible.

You need to open your attachment in a new window, with javascript:

<script>
window.open("yourFileWithAttachment.php");
</script>

Then in the main window you can redirect to another page with header.

header("Location: newPage.php");

You need to open your attachment in a new window, with javascript:

<script>
window.open("yourFileWithAttachment.php");
</script>

Then in the main window you can redirect to another page with header.

header("Location: newPage.php");

Pardon me, If I am wrong but to use the JavaScript from my current php page I will have to echo it out from my php page and since I would have used echo will the header work??

Ok, will not work.
Try a HTML redirect:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Your Page Title</title>
<meta http-equiv="REFRESH" content="0;url=http://www.pageToRedirect.com"></HEAD>
<BODY>
Optional page text here.
</BODY>
</HTML>

Ok, will not work.
Try a HTML redirect:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Your Page Title</title>
<meta http-equiv="REFRESH" content="0;url=http://www.pageToRedirect.com"></HEAD>
<BODY>
Optional page text here.
</BODY>
</HTML>

Ok,
I tried to did what u said but still i am facing problems
I have written

header("Content-Disposition: attachment; filename=$filename");
		echo $out;exit;

and then after closing php tag

<html>
<head>
<title>Your Page Title</title>
<meta http-equiv="REFRESH" content="0;url=<?php $path = "http://" . $_SERVER['SERVER_NAME'] . 
		strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"))."export.php"; echo $path;?>></HEAD>
<BODY>
</BODY>
</HTML>

To redirect to my webpage

But instead of outputting the redirect information on my browser it is outputted on my file, and that too before my

$out

data.
What should I do to correct this

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.