Session_Start error

1

I was copying to a PC that I have, an old system import your database and I went to try it (this is called base_projecto ) it happened that the database had another name different from the one I had put it, change it in connection. (He told me it was base_sancion )

When I login to the system it works, I connect using the username and password, but when I entered the menus errors came from the MYSQL QUERY because the database was not the same, it was actually base_sancion . Next to this without disconnecting the user, change the name of the database from base_project to base_sancion and save, then update the browser

My system works with username and password, from that moment I started to get this error:

  

Warning: session_start () [function.session-start]: Can not send session cache limiter - headers already sent (output started at C: \ wamp \ www \ test_victor \ Sanctions System \ conexion.php: 1) in C : \ wamp \ www \ prueba_victor \ Sanctions System \ home.php on line 4

I think it was that I connected to the system with the database using the first name, then without disconnecting, I changed the database and the connection name and started the error. se creo uno doble sesion?

My system worked well because in the pc where I got it, I tried it before.

This is one of your forms, since they all give the same error but in the line where the beginning of the session is.

Try changing the position of the session, before html, then html, leaving it alone, anyway.

home

 <?php
    include "conexion.php";
    global $cone;
    session_start();
    if(!isset($_SESSION["user_id"]) || $_SESSION["user_id"]==null){
        print "<script>alert(\"Acceso invalido!\");window.location='login.php';</script>";
    }
    ?>
    <html>
        <head>
            <title>.: HOME :.</title>
            <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
        </head>
        <body>
        <?php include "php/navbar.php"; ?>
    <div class="container">
    <div class="row">
    <div class="col-md-6">
            <h2>Bienvenido</h2>
    <?php
    $user_id=$_SESSION["user_id"];
    echo "Usuario: ".$user_id."<br/>";
    $registros=mysqli_query($cone,"select * from user where id=$user_id");
    $reg = mysqli_fetch_array($registros);
    $nombre1 = $reg["nombre1"];
    $nombre2 = $reg["nombre2"];
    $apellido1 = $reg["apellido1"];
    $apellido2 = $reg["apellido2"];
    global $var;
    $var=($nombre1." ".$nombre2." ".$apellido1." ".$apellido2);
    echo $var;
    ?>

    </div>
    </div>
    </div>
        </body>
    </html>


Conexion:

    <?php
    $host="localhost";
    $usua="root";
    $pass="";
    $base="php_practica";

    $cone = new mysqli($host,$usua,$pass,$base);

    if ($cone->connect_errno) {
       echo "Falló la conexión a MySQL: (" . $cone->connect_errno . ") " . $cone->connect_error;    
    } 
    ?>

listing:

<?php
session_start();
include "conexion.php";
global $cone;
if(!isset($_SESSION["user_id"]) || $_SESSION["user_id"]==null)
print "<script>alert(\"Acceso invalido!\");window.location='login.php';</script>";
?>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
<title>Listado del personal sancionado</title>
</head>
<body>
<?php include "php/navbar.php"; ?>
<h1>Listado de Personal</h1>
<div><table align=center>
<thead>
        <tr>
            <th><center>Número</th>
            <th><center>Primer Nombre</th>
            <th><center>Segundo Nombre</th>
            <th><center>Primer Apellido</th>
            <th><center>Segundo Apellido</th>
            <th><center>Cédula</th>
            <th><center>Rango</th>
            <th><center>Opciones</th>
        </tr>
    </thead>
<?php
$contador=0;
$registros=mysqli_query($cone,"select * from personal");
while ($reg = mysqli_fetch_array($registros))
{
$contador=1+$contador;
echo "<td>".$contador."</td>";
echo "<td>".$reg['nombre1']."</td>";
echo "<td>".$reg['nombre2']."</td>";
echo "<td>".$reg['apellido1']."</td>";
echo "<td>".$reg['apellido2']."</td>";
echo "<td>".$reg['cedula']."</td>";
$sql =mysqli_query($cone,"SELECT a.cedula, a.rango_id, b.rango
FROM   personal a LEFT JOIN rangos  b ON a.rango_id = b.id_rango
ORDER BY a.cedula ASC");
$sql2 = mysqli_fetch_array($sql);
echo "<td>".$sql2['rango']."</td>";
echo "<td>"."<a onClick='return confirmSav();' href=actualizar.php?cedula=".$reg['cedula']." >editar<a/>"."</td>";
echo "<td>"."<a onClick='return confirmDel();' href=procesar3.php?cedula=".$reg['cedula']." >eliminar<a/>"."</td>";
echo "</tbody>";
}
?>
</table>
</div>
</body>
</html>
<script>
function confirmSav()
{
  var agree=confirm("¿Desea modificar este registro? ");
  if (agree)
  return true ;
else
   return false ;
}
</script>

<script>
function confirmDel()
{
  var agree=confirm("¿Desea eliminar este registro? ");
  if (agree)
  return true ;
else
   return false ;
}
</script>
    
asked by Victor Alvarado 17.02.2017 в 19:13
source

1 answer

2

The fault is that you have a failure in your conexion.php and that makes the echo ...mensaje de error... jump before session_start()

First you would have to fix the connection failure then you have to see how to handle the failures of these types.

In other words, what do you want to do when you have a failure in the connection to the database, show error to the user? I do not think it's a good idea ... but this is another issue.

  

PHP functions that send or modify HTTP headers should be   run before the requested page has been sent   to the user.

In this answer I have left all the reasons that may cause: headers already sent

Putting session_start(); before include "conexion.php"; solves the problem with header already sent at the moment:

<?php
session_start();
include "conexion.php";
global $cone;
    
answered by 17.02.2017 / 19:38
source