Transfer variables between PHP pages

Try changing your session code as this is the best way to do this.

For example:

index.php

<?php
session_start();

if (isset($_POST['username'], $_POST['password']) {
    $_SESSION['username'] = $_POST['username'];
    $_SESSION['password'] = $_POST['password'];
    echo '<a href="https://stackoverflow.com/questions/240470/nextpage.php">Click to continue.</a>';
} else {
    // form
}
?>

nextpage.php

<?php
session_start();

if (isset($_SESSION['username'])) {
    echo $_SESSION['username'];
} else {
    header('Location: index.php');
}
?>

However I’d probably store something safer like a userid in a session rather than the user’s login credentials.

Leave a Comment