Unable to redirect to page using header( );

Below is the code which is supposed to redirect to a page once the whole form is filled up, the code works like a charm on my XAMPP server (localhost) , but as i uploaded my pages to my hosting area, the page stops redirecting, all it does is come back to the same page as if the page was refreshed !

Also,
i have included the script below to another page using <?php include 'filename.php'; ?>

Please Help!

code :

[php]

Name :

Email Address :

College :


Tell us about your Photographs :
(Optional)

All fields are mandatory

<?php error_reporting(0);

function full_copy( $source, $target ) {
if ( is_dir( $source ) ) {
	@mkdir( $target );
	$d = dir( $source );
	while ( FALSE !== ( $entry = $d->read() ) ) {
		if ( $entry == '.' || $entry == '..' ) {
			continue;
		}
		$Entry = $source . '/' . $entry; 
		if ( is_dir( $Entry ) ) {
			full_copy( $Entry, $target . '/' . $entry );
			continue;
		}
		copy( $Entry, $target . '/' . $entry );
	}

	$d->close();
	
}else {
	copy( $source, $target );
	
}

}

if (isset ($_POST[‘contact_name’]) && isset ($_POST[‘contact_email’]) && isset ($_POST[‘contact_text’]))
{
$rand = rand(1,1000000000);
$contact_name = $rand.$_POST[‘contact_name’];
$contact_email = $_POST[‘contact_email’];
$contact_text = $_POST[‘contact_text’];
$contact_college = $_POST[‘contact_college’];

if (!empty($contact_name) && !empty($contact_email) && !empty($contact_college) )

{
	
	$to = '[email protected]';
	$submit = 'Contact Form Submitted';
	$body = $contact_name."\n".$contact_college."\n".$contact_text;
	$headers = 'From:'.$contact_email;
	
			
	
	if(!is_dir("./uploads/".$contact_name))
	{

	mkdir("./uploads/".$contact_name, 0777, true); //if the directory doesn't exist then make it
	chmod("./uploads/".$contact_name, 0777); 
	//copy('files','uploads/'.$contact_name.'/index.html');
	
	
	
	$source= 'files';
	$target = 'uploads/'.$contact_name;	
	 full_copy($source,$target);
	
	$variable = 'uploads/'.$contact_name.'/'.$contact_name.'.txt';
	
	$handle  = fopen("$variable",'w');
fwrite($handle, "<hr>".'<b>'.'<u>'.'Name:'.$contact_name.'</u>'.'</b>'.' says:'.'<font size="1" >'.'<br />'.'on '.date("l, F d, Y ").'</font>'."\n");
fwrite($handle,"<br />".'<!--'.$contact_email.'-->');
fwrite($handle,"<br />".'<!--'.$contact_text.'-->');
fwrite($handle, "<br />");	
fclose($handle);
			
	
		
	header("Location: uploads/$contact_name/index.html");
	
			
	}
	else
	{
		
		echo'OOPS! Guess there was an error! Please try again! ';
		
		}



}
	 else 
 {
  echo ' <h3>All Fields are Required!</h3>' ;
  	}

}

?>






[/php]

First, why don’t you use E_ALL for error reporting if this is a test…
Second, I think the error is here: “Location: uploads/$contact_name/index.html”. I’m not quite sure you can use variables in a file name, so I would suggest using:
[php]header(“Location: uploads/” . $contact_name . “/index.html”);[/php]

P.S. - I haven’t done PHP in a while and I almost wrote +'s instead of .'s, lol.

Never heard of a webpage starting with a dollar sign! ( $ )

Change: header(“Location: uploads/$contact_name/index.html”);

TO: header(“Location: uploads/” . $contact_name . “/index.html”);

Changed! Yet the same problem!

Hm… Maybe that file doesn’t even exist?

the file is created by the script itself and that also before the calling of the header function, also i manually checked myself that the file exists

Run your script, refresh your FTP connection, check your last-update-date on the file and see if it is actually recreating this file.

You may not be writing over the file as when you make the folder, you are not making it with write access. It may not be actually creating the file.

Let rule that out first. Also, I have found that some servers have a slight write delay and can cause the old file being loaded. (Not often, but, it does happen.)

Last but not least, do a redirect using an existing one that are sure is on. Don’t use a variable until that works, and if it does, there might be something wrong with using a double word…

(I can’t remember about this because, like I said, I haven’t done PHP in a while. Sorry for my dumb-like suggestions. xD)

first of all thanks a lot for the quick replies !

now, if the file wasn’t getting created and the script was not able to detect it then it should go to the page where it says ‘404 file not found’ not coming back to the same page right?

I am being redirected to the same page just as if it refreshed

Well, that depends a lot on your code. If you are redirecting inside of PHP script, it could be that the PHP code is not getting to your redirect command. If this is the case, it might just go back to your current page which would “seem” like a refresh. If you create the page via script, write it to a folder and then jump to it, it should work. But, lots of variables to without all the code in front of us.

This is how I would debug it…

  1. Make sure the PHP code is getting to the redirect code. Just before the redirect, place another one that sends you to a page that is not called by your code. If you get to that page, then the real redirect should be reached by the code. If not, then, your script is not reaching the redirect and you need to find out why.

  2. Make sure that page exists BEFORE calling it. I use the if exists(filename…) type of clause to look for the file and make sure it exists before jumping to it.

  3. Verify, as I said before, that the page is really there and created now by your code. (FTP to your site, run your script, refresh your FTP folder and see if the new file appears with the modified-date being current.

So, these steps need to be done so we can have further info to help you out. Let us know…

Also, you should make sure you are selecting all of the radio buttons/check boxes…

Sponsor our Newsletter | Privacy Policy | Terms of Service