Undefined variable: _SESSION

Asked

Viewed 4,473 times

-1

I can’t seem to solve this problem :/,

Form file.php:

<?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 the $_SESSION in the archive php form., when the user Submit, it stores the url in a $_SESSION and redirects the user to the file x php., down at the archive x php. I try to display the $_SESSION but without success.

Error message: Notice: Undefined variable: _SESSION

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

Esta página não está funcionando
MEUIP enviou uma resposta inválida.
ERR_INVALID_REDIRECT
  • 1

    session_start() is in x.php too?

  • no, when I put this message appears: enviou uma resposta inválida.&#xA;ERR_INVALID_REDIRECT

  • Your.php form file is wrong, it has no opening or closing php tags and is mixed with html revise its syntax

  • @Magichat was just an example is with the correct tags <?php session_start(); ?>

  • So if you want help put with the correct syntax, still wrong.

  • you have a php snippet inside the html form that does not have the php tags, review its syntax...

  • @Magichat already edited, the code is with the tags.

  • At the top of the page x give a var_dump($_SESSION) and glue the result

  • Result is NULL, without the <?php session_start(); ?> on the page x, with the <?php session_start(); ?> on the page x nothing appears but Esta página não está funcionando&#xA;MEUIP enviou uma resposta inválida.&#xA;ERR_INVALID_REDIRECT

  • Have some htaccess redirect?

  • @Magichat I removed a function that makes the redirect and I was able to receive the value, but that’s exactly what I want to do, take the url and put this url in the header

  • Edit your question by placing exactly the code you have and detailing step by step what you want to do.

  • @Magichat Editei

Show 8 more comments

2 answers

1

As you want to access the session variable, you also need to login 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";
  • When I put session_start(); in x.php this message appears: enviou uma resposta inválida.&#xA;ERR_INVALID_REDIRECT

  • The error indicates that the redirect is invalid/non-existent. Check which path the 'urlName' session variable is receiving and whether it makes sense in your project. Another thing, if you are using a server (Apache example), make sure you have given the necessary permissions to access that file.

  • I’m checking here, doing some tests, at least now I already have a "north"

  • From what I saw in the other argument you want to do something like: header('Location: '.$_SESSION['urlName']); right? Check the value of url.

  • exact, I supplemented the code in the topic, now I’m checking the type of value I get in urlName

  • I have already checked the type and the value are correct, the value is the desired url, and the type of it is string, what am I doing wrong? : s

  • When you check in echo $_SESSION["urlName"]; the desired path is correct then? However he accuses error of redirecting, one of the possibilities is that he does not "see" the path/to/o/my/file.php because of permission in the folder (like 755 in Apache). Or the path is out of scope. Since your question was one and the answer was given, and now another problem has arisen, I suggest creating a new question with the suggested problem.

  • the path is correct, so much so that if I copy the result of $_SESSION["urlName"]; ,and paste into the browser bar and der enter, it goes to the location specified in Session, as I resolve this deal to allow 755 apache?

  • About Apache I just quoted an example, because it wasn’t clear if you were using any server. If you are building your application inside the folder *var/www Apache, you need to search for access permissions with the commands chmod, chown (reading, writing etc) of your files and folders, and what is the best way for your project and how to make these changes according to your operating system. But the problem doesn’t seem to be this and I can’t see how to help you.

  • All right, thanks anyway, you and the others helped a lot, I’ll create another topic with the new problem, and yes I’m using apache xampp/htdocs/nameProject/index.php

Show 5 more comments

-1

First you do a simple test. Create two files, for example, a.php and b.php, on the first boot 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'];
?>

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

  • Thanks for your help, the discussion now is, because I get the value $urlName = "www.google.com"; in my Session the value type is string, and when I pass to that function, function pageRedirect($urlName){&#xA; header("Location: " . $urlName);&#xA; die();&#xA;} of that error message : Esta página não está funcionando&#xA;192.168.0.70 enviou uma resposta inválida.&#xA;ERR_INVALID_REDIRECT OBS: the google url was just an example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.