0

I have a register.php page. I sent the data through a form to a php_register.php file. this file check that the form is valid (the email is real and such things). I want that the php_register.php will do his job and send to client back the register.php file in case of invalid form:

header("Location: register.php");

I want that php_register.php will send additional data to a register.php. in my case, I want to send what went wrong on the input. I can do it like this:

header("Location: register.php?error=5");

Through register.php $_GET['error'] I can display the right output. What I want to do is the same thing, but instead get method, use post methods (without any forms involve) ? How can I do it?

Shelef
  • 3,707
  • 6
  • 23
  • 28
  • Add the posted values in $_SESSION, these u can access throughout the entire website. Don't forget to add session_start() at line 1 – DarkBee Apr 29 '13 at 17:24
  • Relevant: http://programmers.stackexchange.com/questions/99894/why-doesnt-http-have-post-redirect – Marc B Apr 29 '13 at 17:30

2 Answers2

3

The long and the short is that you can't. The internet doesn't work that way. Honestly, a browser could even ignore header if it wanted to (which is why header should, without exception, be followed by die, exit, or some other function which forces termination of the script.).

You can store the values in $_SESSION, or you can have it POST to the same page the form is on (which is how most frameworks handle this), but you can't tell the browser "Submit this POST somewhere else."

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • http status code 307 - redirect using same method/data as original request. it WILL redirect a post properly. but it's potentially unsafe to do so. – Marc B Apr 29 '13 at 17:32
  • not commonly used, because POSTS generally should not be repeatable. e.g. "delete last user added" might work fine the first time. reload that page/redirect 2 hours later and you're probably nuking yet another user. – Marc B Apr 29 '13 at 17:36
0

Use sessions, for example in php_register.php:

// start the session
session_start();

// Email check (do a more robust check of course)
if($_POST['email'] == "")
{
  $_SESSION['error'] = "Please enter an email address."
  header("Location: register.php");
  die();
}

then on register.php

// start session
session_start();

if(isset($_SESSION['error']))
{
  echo $_SESSION['error'];

  // unset now so it doesn't show again
  unset($_SESSION['error']);

}
Springie
  • 718
  • 5
  • 8