1

I'm trying PHP session where the user will input an Email in page 1 . After they've entered , they'll be redirect to page 2 with the email displayed as the user.

Here's my page 1 PHP code looks like :

<?php
session_start();
$_SESSION['email']= isset($_POST['email']) ;
if(isset($_POST['submit'])){

header('Location: http://localhost/page2');
}   
?>
<form method="post" action=""> 

Email: <input type="text" name="email"  value=""/> <br/><br/>
<input type="submit" name="submit"/> </form>

Page 2 :

<?php
session_start();
$email = (isset($_SESSION['email']));
echo $_SESSION['email'];
//print_r($_SESSION);
//var_dump($email);
?>

Problem:

The problem is , when I entered any random emails , it shows number 1 in page 2 when I echoed it.

1
  • isset is a boolean function. Commented Nov 27, 2014 at 4:35

4 Answers 4

2

That' because you assign the return value of the function isset()!

So this should work for you:

if(isset($_POST['submit']) && !empty($_POST['email'])){
    $_SESSION['email']= $_POST['email'];
    header('Location: http://localhost/page2');
}   

Also on your second page you assign it again which you don't have to! You have to check it like this:

if (isset($_SESSION['email']))
    echo $_SESSION['email'];

(BTW: I hope this is more an example: header('Location: http://localhost/page2'); because you would have to add the file extension like: header('Location: http://localhost/page2.php');)

Sign up to request clarification or add additional context in comments.

1 Comment

Up voted this because of the explanation. OP is assigning the return value of the isset() function to the variable, not the actual value. I assume he wanted something like this: $_SESSION['email'] = (isset($_POST['email'])) ? $_POST['email'] : '';
0

The statement below assign TRUE or FALSE to the session variable as isset() returns boolean values not the string passed.

But, you need to assign the value to it.

Simply change:

$_SESSION['email']= isset($_POST['email']);

To

if (isset($_POST['email'])) {
  $_SESSION['email']= $_POST['email'];
}

Comments

0

On Page 1

<?php
session_start();
if(isset($_POST['submit'])){
    header('Location: http://localhost/page2');
}   
?>

<form method="post" action="http://localhost/page2"> 
Email: <input type="text" name="email"  value=""/> <br/><br/>
<input type="submit" name="submit"/> 
</form>

On Page 2

<?php
session_start();
$_SESSION['email'] = $_POST['emai'];
echo $_SESSION['email'];
?>

Comments

0

Page 1

<?php
session_start();
if(isset($_POST['submit'])) {
$_SESSION['email'] = $_POST['email'];
header('Location: http://localhost/page2');
}   
?>

<form method="post" action=""> 
Email: <input type="text" name="email"  value=""/> <br/><br/>
<input type="submit" name="submit"/> 
</form>

Page 2

<?php
session_start();

//check if session exist
if(isset($_SESSION['email'])) {
echo $_SESSION['email'];
}

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.