My File Session publicacoes.php reaches null until follow.php

Asked

Viewed 14 times

-1

simplified twitter replica

ERROR

Warning: Cannot Modify header information - headers already sent by (output Started at /Storage/ssd2/125/17032125/public_html/follow.php:3) in /Storage/ssd2/125/17032125/public_html/follow.php on line 26

Explanation of functionality

If I click on following any user in the.php publishing file is redirected by action to another file (follow.php) with get parameter and the following user id.

If the parameter is from, it activates a follow function, and if not it stops following (with a query between two id "follower_id and user_id").

Recurrent error

when I click on follow happens header error 1. However functions with database continue to function normally and the user follows and stops following and only appears if you go back to page.

follow.php

<?php
session_start();
print "<pre>";
print_r($_SESSION);
include_once("header.php");
include_once("functions.php");
$id = $_GET['id'];
$do = $_GET['do'];

switch ($do){
    case "follow":
        follow_user($_SESSION['userid'],$id, $db);
        //$msg = "You have followed a user!";
    break;

    case "unfollow":
        unfollow_user($_SESSION['userid'],$id, $db);
        //$msg = "You have unfollowed a user!";
    break;

}
//$_SESSION['message'] = $msg;
// futuramente imprimir com um framework uma caixa na tela dizendo q esta seguindo

header("location: publicacoes.php");
?>

Excerpt from the publications.php

<?php 
      // imprime os usuários e se quer seguir ou não
      $users = show_users($db);
      $following = following($_SESSION['userid'], $db);

      foreach ($users as $key => $user) {
        print "<p class=\"nome-perfil-comentario\">". $user ."</p>";
        if (in_array($key, $following)){
          $_GET['id'] = $key;
          $_GET['do'] = 'follow';
              print"<a nome=\"follow\" href=\"follow.php?id=$key&do=unfollow\" class=\"botao-seguir w-inline-block\">
                      <p class=\"seguir\"><small>Não seguir</small></p>
                    </a>";      
        } else {
          
            print"<a href=\"follow.php?id=$key&do=follow\" class=\"botao-seguir w-inline-block\">
              <p class=\"seguir\"><small>Seguir</small></p>
            </a>";  
        }
      }
                                        
      ?>

1 answer

-2

The PHP error is explicitly saying what is wrong:

Cannot Modify header information - headers already sent by( output Started at ...

Come on...

What does that mean?

Function header() according to PHP documentation https://www.php.net/manual/en/function.header.php cannot contain any source output, lines, spaces, html, etc., before it is called.

Remember that header() must be called before any actual output is sent, either by normal HTML tags, Blank Lines in a file, or from PHP.

What’s wrong with your code

The error says: output Started at follow.php:3

Line 3 of your code in the follow.php file has a print (output) and, again, according to the PHP documentation function header() cannot contain any output before being called.

Line 4 also has a print_r, which should be removed if Voce is calling the function header()

... in follow.php on line 26

Your code will execute normally, but when you get to line 26 the function header() will not work...

How to solve

PHP errors are very straightforward, and your code is well structured, it will tell you exactly where the problem is.

Basically the error showed explicitly what is going wrong in its code : Cannot modify header information - headers already sent by ..., then Voce should remove any output before calling the function header()

:)

Browser other questions tagged

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