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 :

<form action="upload_images.php" method="post">
  Name<span style="font-size: 10px"> </span>: <br/>
  <input type="text" name="contact_name" maxlength="25"><br />
  <p>Email Address<span style="font-size: 10px"> </span>:<br />
  <input  type="email" name="contact_email" maxlength="40">
  <p>College :<br />
  <input type="text" name="contact_college">
  <br />
  <br />
  Tell us about your Photographs :<br />
  <span style="font-size: 11px">(Optional)</span><br />
  <textarea name="contact_text" cols="60" rows="10"></textarea>
  <br>
  <span style="font-size: 12px">All fields are mandatory </span>
  <br>
  <br>
  <?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 = 'contact@pingcampus.com';
		$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>' ;
      	}
}


?>
  <br>
<input type="submit" value="Proceed!">
  <br>
  <br>
</p>
<p>
<p>
<p>
<p>
<p>
<p>
<p>
<p>
<p>
<p>
<p>
</form>
</p>
Member Avatar for iamthwee

My immediate guess would be to check the file permissions on you hosting folder.

How can i do that?

error_reporting(0); -> error_reporting(E_ALL);

Oh it's obvious now i think about it, you have to call a header before ANY output - the php needs to start on line 1 with no white space and then you cannot print or echo any data out until the header is called or the header will error and won't work. (headers come before any output)

so you mean that i can't even have an form before the <?php tag? and also i have checked that i am not giving out any outputs before the header function.

as in the code above i have used the header inside an if statement , that is allowed i guess please see in the header calling , am i using it correct ?

once again, it works perfectly on XAMPP but not on hosting :(

yes a header has to be before any output, once you output something headers are automatically sent and can't be edited after that. This also includes the page that is including this page, the page including this one can't output any text before the header is set or if will fail.

You solve it by putting the response message into a variable to use later like this:

<?php
error_reporting(E_ALL);

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 = 'contact@pingcampus.com';
		$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{
			$message = 'OOPS! Guess there was an error! Please try again! ';
		}
	}else{
	  $message = ' <h3>All Fields are Required!</h3>' ;
	}
}
?>
<form action="upload_images.php" method="post">
  Name<span style="font-size: 10px"> </span>: <br/>
  <input type="text" name="contact_name" maxlength="25"><br />
  <p>Email Address<span style="font-size: 10px"> </span>:<br />
  <input  type="email" name="contact_email" maxlength="40">
  <p>College :<br />
  <input type="text" name="contact_college">
  <br />
  <br />
  Tell us about your Photographs :<br />
  <span style="font-size: 11px">(Optional)</span><br />
  <textarea name="contact_text" cols="60" rows="10"></textarea>
  <br>
  <span style="font-size: 12px">All fields are mandatory </span>
  <br>
  <br>
  <?php echo $message;?>
  <br>
<input type="submit" value="Proceed!">
  <br>
  <br>
</p>
<p>
<p>
<p>
<p>
<p>
<p>
<p>
<p>
<p>
<p>
<p>
</form>
</p>

try, look for javascript redirect. The only problem with this approach is that it will not work if javascript is turned off.

echo '<script type="text/javascript">
<!--
window.location = "http://YourDomainDotCom/uploads/$contact_name/index.html";
//-->
</script>';

Otherwise, you must end the script just right after your redirection code. Something like this

header("Location: uploads/$contact_name/index.html");
exit;

## all the codes below this will be ignored. Unless, you rearrange your statements, so that your redirection codes will be the last.
commented: useful +3

still doesn't helps , as i said i have included this php file on a page called upload_images.php, to which the form is action at , is that any issue?

Redirection should be in the page or the script location where the uploads are being processed.

Example flowchart

User Upload images --> User Click Submit Button --> Script Post Data to processor.php --> Processor.php confirm uploads -->Processor.php redirects user --> end of script.

what i am trying to make it work like is this :

user enter the form details -> folders are created accordingly -> files are copied to the created folders -> redirection to one of the copied file

still doesn't helps , as i said i have included this php file on a page called upload_images.php, to which the form is action at , is that any issue?

Will have to see everything in that page up to the include as well(take out passwords,emails etc) or check through yourself and make sure nothing is printed out before it.

You could also use javascript like veedeoo suggested, that will be very easy to put in, javascript is very standard nowadays

i tried using the javascript as well, it is redirecting the page for sure, but then the problem is that is is redirecting u the url

http://localhost/uploads/$contact_name/index.html

rather than using whatever is stored inside variable $contact_name

any help on this??

what i am trying to make it work like is this :

user enter the form details -> folders are created accordingly -> files are copied to the created folders -> redirection to one of the copied file

ok, now all of us are looking at the bright picture. Sometimes, going into the basic of flow charting helps..

On your codes where it says

$variable = 'uploads/'.$contact_name.'/'.$contact_name.'.txt';

While you are redirecting your user to

uploads/$contact_name/index.html

Shouldn't be these codes?;

$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);

be changed to

$variable = 'uploads/'.$contact_name.'/index.html';
$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);

Why these changes? Because we cannot redirect your user to something that may not exist in the uploads/$contact_name/


If you still want to redirect your user to the actual text file created by your script, then the redirection codes should be

'uploads/'.$contact_name.'/'.$contact_name.'.txt';

This will take your user to the text page view or review the processed data.

thanks for reading the complete code!,

see i dont want to redirect to the txt file where data is written,

what actually should happen is:

when the user submits the data, the file is created, from a location certain files are copied to the newly created file, and then one of those files is needed to be redirected to!

so here

uploads/$contact_name/index.html

here the index.html file is the file which is copied and i want to redirect to! The txt file is of no use to the user it is for my site admins to be used as a log thats it

ok,.. please do me a favor.. Run your script one more time.. Try to create a new user. On your server, check if a directory named after the new user is created. Also, you may want to check if the index.html exist in that directory.


I suspect that php copy function is might be faulty on your server side, but this is just one of the test we need to make in order to isolate where the problem is coming from. If everything is well, then we can go back to tackling the redirection problem.

yeah i checked that the files are being created perfectly ! Also i checked that is my hosting allowing the use of header(); and yes they are ,

i just turned off error reporting and now i got this warning message:

Warning: Cannot modify header information - headers already sent by (output started at /hermes/bosweb/web054/b548/ipg.pingcampuscom/image-upload/upload-form.php:3) in /hermes/bosweb/web054/b548/ipg.pingcampuscom/image-upload/upload-form.php on line 93


so somewhere some output is being given , but i am jsut able to catch it out where X_X can u please read the code once and look for me ?? PLEASE!

also as u mentioned about the javascript , i am okay to use that as well, only if it uses the value that is in $contact_name rather than $contact_name itself in the url

yeah i checked that the files are being created perfectly ! Also i checked that is my hosting allowing the use of header(); and yes they are ,

i just turned off error reporting and now i got this warning message:

Warning: Cannot modify header information - headers already sent by (output started at /hermes/bosweb/web054/b548/ipg.pingcampuscom/image-upload/upload-form.php:3) in /hermes/bosweb/web054/b548/ipg.pingcampuscom/image-upload/upload-form.php on line 93


so somewhere some output is being given, but i am jsut able to catch it out where X_X can u please read the code once and look for me ?? PLEASE!

We'll need to see the code on that page for upload-form.php - taking out any passwords, ips etc so hackers don't start trying to get in(remote chance but better safe).

Javascript is a work around its not optimal but for getting things working for now its a very nice shortcut

echo '<META HTTP-EQUIV="Refresh" Content="0; URL=filename.php">';
exit;
You can use this as a alternate

commented: good alternative +3
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.