0

I have a script to connect to my db in MYSQL and I use PDO to connect. So When I use session_start it keeps reloading my page but when comment it //session_start(); it works perfectly. what do I do wrong?

Code

<?php

ob_start(); 
//session_start();

//$_SESSION['already_submitted'] = false;

$timezone = date_default_timezone_set("Europe/London");

$DB_host = "localhost";
$DB_user = "root";
$DB_pass = "";
$DB_name = "db_name";

// Set the credentials for the database and make a new PDO connection,
// if the connection fails display the error.
try
{
 $DB_con = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass);
 $DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
 echo $e->getMessage();
}

include_once 'class.crud.php';

$crud = new crud($DB_con);

?>

class.crud.php (just a class to help me use the crud more rapidly file)

<?php
class crud
{
    private $db;

    public function __construct($DB_con)
    {
        $this->db = $DB_con;
    }

    public function getID($id, $mod_name)
    {
        $stmt = $this->db->prepare("SELECT * FROM $mod_name WHERE id=:id");
        $stmt->execute(array(":id" => $id));
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        return $row;
    }

    public function getPassword($contrasena)
    {
        $stmt = $this->db->prepare("SELECT * FROM users WHERE username=:username");
        $stmt->execute(array(":username" => "Fred"));
        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        if (!password_verify($contrasena, $row['password'])) {
            ?>
            <div class="uk-alert uk-alert-danger" data-uk-alert>
            <a href="#" id="hide-alert" class="uk-alert-close uk-close"></a>
            <strong>Error!</strong> Contraseña.
            </div>

            <script>
            function show_alert() {
            $("#hide-alert").click();
            }

            window.setTimeout(function () {
            show_alert();
            }, 5000);
            </script>
            <?php
} else {

            // Success!

            return $row;
        }
    }

//combobox
    public function select($column, $mod_name)
    {
        $stmt = $this->db->prepare("SELECT $column FROM $mod_name ORDER BY id");
        $stmt->execute();

        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            echo '<option value="' . ucwords(strtolower($row['firstname'])) . ' ' . ucwords(strtolower($row['lastname'])) . '">' . ucwords(strtolower($row['firstname'])) . ' ' . ucwords(strtolower($row['lastname'])) . '</option>';
        }
    }

    public function insert($table, $data)
    {
        ksort($data);

        $fieldNames = implode('`,`', array_keys($data));
        $fieldValues = ':' . implode(', :', array_keys($data));

        $stmt = $this->db->prepare("INSERT INTO $table (`$fieldNames`) VALUES ($fieldValues)");

        foreach ($data as $key => $value) {
            $stmt->bindValue(":$key", $value);
        }

        if (!$stmt->execute()) {
            ?>
            <div class="uk-alert uk-alert-success" data-uk-alert>
            <a href="#" id="hide-alert" class="uk-alert-close uk-close"></a>
            <strong>Error!</strong> Registro no agregado,
            <?php $this->handleError();
            echo $stmt->errorInfo();
            ?>.
            </div>

            <script>
            function show_alert() {
            $("#hide-alert").click();
            }

            window.setTimeout(function () {
            show_alert();
            }, 5000);
            </script>
            <?php
} else {
            ?>

            <div class="uk-alert uk-alert-success" data-uk-alert>
            <a href="#" id="hide-alert" class="uk-alert-close uk-close"></a>
            <strong>Exito!</strong> Registro agregado.
            </div>

            <script>
            function show_alert() {
            $("#hide-alert").click();
            }

            window.setTimeout(function () {
            show_alert();
            }, 5000);
            </script>
            <?php
}
    }

    public function update($table, $data, $where)
    {
        $table;
        ksort($data);

        $fieldDetails = null;
        foreach ($data as $key => $value) {
            $fieldDetails .= "`$key`=:$key,";
        }
        $fieldDetails = rtrim($fieldDetails, ',');

        $stmt = $this->db->prepare("UPDATE $table SET $fieldDetails WHERE $where");

        foreach ($data as $key => $value) {
            $stmt->bindValue(":$key", $value);
        }

        if (!$stmt->execute()) {
            ?>
        <div class="uk-alert uk-alert-success" data-uk-alert>
        <a href="#" id="hide-alert" class="uk-alert-close uk-close"></a>
        <strong>Error!</strong> Registro no actualizado,
        <?php $this->handleError();
            echo $stmt->errorInfo();
            ?>.
        </div>

        <script>
        function show_alert() {
        $("#hide-alert").click();
        }

        window.setTimeout(function () {
        show_alert();
        }, 5000);
        </script>
        <?php
} else {
            ?>

        <div class="uk-alert uk-alert-success" data-uk-alert>
        <a href="#" id="hide-alert" class="uk-alert-close uk-close"></a>
        <strong>Exito!</strong> Registro actualizado.
        </div>

        <script>
        function show_alert() {
        $("#hide-alert").click();
        }

        window.setTimeout(function () {
        show_alert();
        }, 5000);
        </script>
        <?php
}
    }

    public function delete($table, $data, $where, $limit = 1)
    {

        $stmt = $this->db->prepare("DELETE FROM $table WHERE $where LIMIT $limit");
        foreach ($data as $key => $value) {
            $stmt->bindValue(":$key", $value);
        }
        if (!$stmt->execute()) {
            ?>
            <div class="uk-alert uk-alert-success" data-uk-alert>
            <a href="#" id="hide-alert" class="uk-alert-close uk-close"></a>
            <strong>Error!</strong> Registro no elminado,
            <?php $this->handleError();
            echo $stmt->errorInfo();
            ?>.
            </div>

            <script>
            function show_alert() {
            $("#hide-alert").click();
            }

            window.setTimeout(function () {
            show_alert();
            }, 5000);
            </script>
            <?php
} else {
            ?>

            <div class="uk-alert uk-alert-success" data-uk-alert>
            <a href="#" id="hide-alert" class="uk-alert-close uk-close"></a>
            <strong>Exito!</strong> Registro eliminado.
            </div>

            <script>
            function show_alert() {
            $("#hide-alert").click();
            }

            window.setTimeout(function () {
            show_alert();
            }, 5000);
            </script>
            <?php
return true;
        }
    }

    /* error check */
    private function handleError()
    {
        if ($this->errorCode() != '00000') {
            if ($this->_errorLog == true)
            //Log::write($this->_errorLog, "Error: " . implode(',', $this->errorInfo()));
            {
                echo json_encode($this->errorInfo());
            }

            throw new Exception("Error: " . implode(',', $this->errorInfo()));
        }
    }

    /* count rows*/
    public function rowsCount($table)
    {
        $stmt = $this->prepare("SELECT * FROM " . $table);
        $stmt->execute();
        return $stmt->rowCount();
    }
}
Daniel
  • 8,788
  • 11
  • 33
  • 68
Nehoss
  • 33
  • 5
  • can you show the code of class.crud.php also – sietse85 Aug 08 '18 at 17:42
  • Yes, absolutely, I'll edit my question. – Nehoss Aug 08 '18 at 17:46
  • Perhaps try to check if the session is already started on page load, and if it's not, load it? https://stackoverflow.com/a/18542272/5827005 - Maybe it will help. If you are using a PHP framework, you shouldn't need to turn on sessions on every page but in your single global file. – GrumpyCrouton Aug 08 '18 at 18:10
  • Thanks, but it's a single page index.php and I include my connection file "config.php" in it so I'm pretty sure it is not started before I load my index page. But anyways I tried your solution and does not work. – Nehoss Aug 08 '18 at 18:33

1 Answers1

0

I found the answer here Pages wont load if it has session_start(); in it.

The problem lies in xampp and php 7, I change it for wamp and it works fine.

Hope it helps someone !

Nehoss
  • 33
  • 5