What is the correct way to use the header location? [closed]

3

I'm doing a PHP and MySQL course on Alura and got to the part where we built the code that removes the product from the database.

The page that calls the delete function must redirect the user back to the produto-lista.php page and the course instructor suggested the following code:

<?php
  include("cabecalho.php");
  include("conecta.php");
  include("banco-produto.php");

  $id = $_GET['id'];
  removeProduto($conexao, $id);
  header["location: produto-lista.php"];
?>

It turns out that this way the product is yes being removed, but the header location is generating me the following error:

  

Notice: Use of undefined constant header - assumed 'header' in   C: \ xampp \ htdocs \ store \ remove-product.php on line 8

     

Warning: Illegal string offset 'location: product-list.php' in   C: \ xampp \ htdocs \ store \ remove-product.php on line 8

I'm using Xamp for Windows.

    
asked by anonymous 14.01.2016 / 19:31

2 answers

8

Instead of header["location: produto-lista.php"]; put header("location: produto-lista.php"); die('Não ignore meu cabeçalho...');

The die() is important so that nothing is executed after this command, thus avoiding some unexpected error, could use exit() as well.

    
14.01.2016 / 19:32
6

The error that is in the code you posted in the question is in the way you are calling the header function, where instead of using parentheses you are using brackets.

Wrong form:

header["location: produto-lista.php"];

Correct form:

header("location: produto-lista.php");
Note also that the header function must always be used before any data output sent, as it manipulates the header of the HTTP request sent.

    
14.01.2016 / 19:52