Getting re-directed after "Submit"

Discussion in 'PHP' started by chrisj, Sep 13, 2007.

  1. #1
    I'm using an upload script, that works fine. I simply want to re-direct the user once he selects the Submit button. I was told to add

    header("Location: http://www.somelocation.com");

    at the bottom of the file, right after: return $message;

    Did it. But the user does not get re-directed.

    I tried this too, without success:

    return $message;
    header("Location: http://www.somelocation.com");
    echo $message;

    Here's the bottom part of the script page. Any help will be greatly appreciated. If you need the whole page code, let me know.
    Thanks.

    
    function upload_file($upload_directory, $upload_uri) {global $account;
            $file_name = $_FILES["userfile"]["name"];
    	$file_name = str_replace(" ","_",$file_name);
    	$ext=pathinfo($file_name,PATHINFO_EXTENSION);
            $file_name = basename($file_name, '.' . $ext);
            $file_path = $upload_directory. $file_name.'~~'. $account->get_user_name().'.'.$ext;
    
    	$temporary = $_FILES["userfile"]["tmp_name"];
    
    	$result = move_uploaded_file($temporary, $file_path);
    	if(!chmod($file_path,0777))
    	$message = "ERROR: A folder to place the files was not found, or the files need to be CHMODed to 777.";
    	else $message = ($result)?"File has been uploaded." : "An error has occurred.";
    
    
    	return $message;
            header("Location: http://www.somelocation.com");
    	}
    ?>
    
    Code (markup):
     
    chrisj, Sep 13, 2007 IP
  2. k2pi

    k2pi Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Be carreful, header function works only if it is the first thing displayed on your page.

    You can put as much php treatment code as you want before 'header', but you can't displayed anything (don't use echo for example).

    A simple space character at the top of the page is enough to prevent the header redirection.

    hope it'll help you.
     
    k2pi, Sep 13, 2007 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    If you have a return call before your header, the header will never get called.

    
    
    return $message; //the return here is killing the script
    header("Location: http://www.somelocation.com"); //this doesn't get executed.
    
    PHP:
    You need to either remove the return, or put the header above it.

    
    	$result = move_uploaded_file($temporary, $file_path);
    	if(!chmod($file_path,0777))
    	$message = "ERROR: A folder to place the files was not found, or the files need to be CHMODed to 777.";
    	else $message = ($result)?"File has been uploaded." : "An error has occurred.";
    
    
            header("Location: http://www.somelocation.com");
    	}
    
    PHP:
    Also, you should use brackets {} as it is a much more acceptable coding practice, and far easier to quickly determine what a script is doing.
     
    jestep, Sep 13, 2007 IP