Problem with PHP+Apache XAMPP redirection

Asked

Viewed 1,058 times

1

The problem is this:

Form file.php:

<?php session_start(); ?>

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

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

      echo $_SESSION["urlName"];
  ?>

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

x.php file:

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

    $urlName = $_SESSION["urlName"];

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

pageRedirect($urlName);

File redirect.php:

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

Value received on $urlName:

"192.168.x. xx:xxx/project/site-x/index.php"

Type: String

ERROR MESSAGE:

This page is not working 192.168.x. xx sent an invalid reply. ERR_INVALID_REDIRECT

*

OBS: Is not duplicate of Undefined variable: _SESSION, are different issues.

Hosts file:

# copyright (c) 1993-2009 microsoft corp.
#
# this is a sample hosts file used by microsoft tcp/ip for windows.
#
# this file contains the mappings of ip addresses to host names. each
# entry should be kept on an individual line. the ip address should
# be placed in the first column followed by the corresponding host name.
# the ip address and the host name should be separated by at least one
# space.
#
# additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# for example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within dns itself.
#   127.0.0.1       localhost
#   ::1             localhost
  • @Fernando that post was on the subject of Session, this is about the redirect error, the staff of the other post who advised me to create a new topic with this new problem

  • You have access to apache files, what your operating system is?

  • @Magichat SO: Windows, I think so.

  • How are you C:/WINDOWS/system32/drivers/etc/hosts?

  • @Magichat edited the topic, put the content of hosts in it.

  • Uncomment the line of localhost.

  • 127.0.0.1 localhost and restart the apache.

  • @Magichat solved some things, now the problem is another :s, it redirects, however it does not erase the url that was before, it just complements it. example: it goes to the url: localhost/projetos/site/x.php, and when you run the function, this is the url localhost/projetos/site/x.php/localhost/index.php, and the goal is just to redirect to localhost/index.php

  • @Magichat ARRUMEEEEEEEEI Finally kkkkk, our, how bad it is not to know how to do things kkkk, thanks for the help Magic, it was no problem in apache no, there were 2 errors, 1 was that there was session_start in the form.php(yes I had put, I must have erased to test and I ended up not returning him), and the other error dps to have fixed this, was that in header("Location: " . $urlName);, had to put the http:// header("Location: http://" . $urlName);

Show 4 more comments

1 answer

1

This problem occurs because the function echo changes the response header and then you can no longer use the function header(). In this the ideal would be not to use the function echo to be able to use the function header() normally.

  • I removed echo, http:// and the error continued :s.

  • still needs the http://... because you are redirecting to a url, redirect only to a file (without the 192.168.x.xx:xxx/ ..) would not be necessary.

  • How do I do it?

  • 1

    You can only pass the relative file path as you do when using require.

  • aaa know, I’ll see here if I modify and give a feedback ^^

  • Have you thought about implementing your function to check if the url has http/https and automatically add it, if not? You can do it lightly with regular expressions. Kd feedback?

Show 1 more comment

Browser other questions tagged

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