How to use the Location function in object orientation

Asked

Viewed 147 times

0

I am trying to direct the user to the panel page, when he login on the home page, but when directing gives the error below, but if I use javascript with the same path it works.

Using the Location: inserir a descrição da imagem aqui

When I use the line of code below I can redirect, but I think it’s better to use Location.

inserir a descrição da imagem aqui

  • Avoid posting the image of the code. Post the code itself. [mcve]

  • I try but when it’s HTML code it doesn’t appear on the screen when saved

  • When pasting the code just select it and press Ctrl + K

  • @Andersoncarloswoss problems are different. In the given question the error is Cannot modify header. In this question, the error is due to the redirect working as expected in the JS, but not redirect as expected when using the php

  • @Valdeirpsr no, the problem is exactly that. The question is that the author omitted from the question the error message that he himself quoted.

Show 1 more comment

1 answer

0

The url is not equal as commented on in the question

Note the lines below:

Header:

header("Location:/views/painel.php");

Html

echo "<meta http-equiv='refresh' content='0;URL=views/painel.php' />";

Note that in the header you are adding a / (bar) before the URL. When you add this bar at the beginning, it means that the user will have to be redirected to <raiz-do-projeto>/views/painel.php

Without the slider, you inform that the user must be redirected to part of the current URL + address. Ex:


Imagine that you are in the administration area of the site: https://www.example.com/admin/

When you access with the bar at the beginning (as in header), you are informing the user to be redirected to https://www.example.com/views/painel.php

When you access without the bar at the beginning (as in html), you are informing the user to be redirected to https://www.example.com/admin/views/painel.php


Another error in your code is the echo before the Location.

When you write I printed something with echo, print etc. (or even there are "hidden" characters before the tag opens <?php). The PHP cannot modify information from header.

Browser other questions tagged

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