Undefined variable: _SESSION

-1

I can not solve this problem: /,

Form.php file:

<?php session_start(); ?>

<form action="x.php" method="post">

  <?php  
      $_SESSION["urlName"] = $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"];

      echo $_SESSION["urlName"]; // o valor é exibido aqui no formulario.php.
  ?>

    <input type="email">
    <button type="submit"></button>
</form>

x.php file:

<?php
    require("redirect.php");

    $urlName = $_SESSION["urlName"]; //o valor não chega aqui no x.php, Pq?

    if(isset($_SESSION["urlName"]))
        echo $urlName;
    else
        echo "URL NÃO encontrada";

pageRedirect($urlName);

File redirect.php:

function pageRedirect($urlName){
    header("Location: " . $urlName);
    die();
}

Summarizing the code:

I open $_SESSION in the form.php file, when the user submit , it stores the url in a $_SESSION and redirects the user to the x.php file, there in the x.php file I try to display the $_SESSION stored, but to no avail.

Error message: Notice: Undefined variable: _SESSION

If I put session_start (); in the x.php file too, the error message changes:

Esta página não está funcionando
MEUIP enviou uma resposta inválida.
ERR_INVALID_REDIRECT
    
asked by anonymous 09.01.2018 / 15:50

2 answers

1

As you want to access the session variable, you also need to log in to both files:

session_start();

$urlName = $_SESSION["urlName"]; //o valor não chega aqui no x.php, Pq?

if(isset($_SESSION["urlName"]))
    echo $urlName;
else
    echo "URL NÃO encontrada";
    
09.01.2018 / 16:28
-1

First you do a simple test. Create two files, for example, a.php and b.php , in the first initialize some session variable, in the second try to access it.

a.php

<?php
session_start();

$_SESSION['teste'] = 'Funcionou';
echo $_SESSION['teste'];
?>

b.php

<?php
session_start();

echo $_SESSION['teste'];
?>

Access a.php first and then go to b.php, and see if it works.

    
09.01.2018 / 16:54