-2

I am coding a login page with curl, so I need to get some variables to the next page called action.php.

Here is my code:

global $var1;
global $var2;

if(isset($match[1][0])){
mail($send,$subject,$message,$headers);
header('Location: page1.php');}else{
header('Location: page2.php');}

page1.php

require('action.php');

echo $var1;

echo $var2;

it's not getting me the variables.

and also i see

Warning: Cannot modify header information - headers already sent by (output started at on page1.php

any suggestion?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Have a look at this: http://stackoverflow.com/questions/8028957/headers-already-sent-by-php – Quasdunk Apr 12 '13 at 12:27
  • thanks but it didn't fix passing the $vars – user2255821 Apr 12 '13 at 12:34
  • My suggestion is that you fix the error. How to fix it depends on what you try to achieve. That is not clear from the (incomplete) code in your question. And it sounds more like a support request than a programming question. – M8R-1jmw5r Apr 12 '13 at 12:40

3 Answers3

0

If you have any content already written to the screen then you cannot use a header redirect.

So even if you're doing echo "hello" or have <html> before it the header command won't work either use javascript redirects for after output redirects or use output buffering which will allow you to do post output redirects as it'll only write out the buffer when you tell it to.

Hope that makes sense

Dave
  • 3,280
  • 2
  • 22
  • 40
0

This is in PHP Manual,

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

See for yourself where your are going wrong.

EDIT: You can pass the variable by setting the value to $_SESSION[] and reading them on the other side.

Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47
0

Your question about the warning has been answered (don't send any output before using header()), but the issue of the data not being passed between pages has not.

A global variable will not be accessible between 2 PHP scripts - HTTP is stateless.

Use a session or some other means to make the data accessible between both pages.

session_start();
$_SESSION['var1'] = 'some value';
$_SESSION['var2'] = 'some value';

if(isset($match[1][0])){
    mail($send,$subject,$message,$headers);
    header('Location: page1.php');
}else{
    header('Location: page2.php');
}

and

session_start();
require('action.php');
echo $_SESSION['var1'];
echo $_SESSION['var2'];
Community
  • 1
  • 1
Nerdwood
  • 3,947
  • 1
  • 21
  • 20