Session_start保持重新加载页面

时间:2018-08-08 17:40:51

标签: php database pdo

我有一个脚本可以连接到MYSQL中的数据库,并使用PDO进行连接。因此,当我使用session_start时,它会不断重新加载我的页面,但是当对它进行//session_start();注释时,它会完美地工作。我该怎么办?

代码

<?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(只是一个类,可以帮助我更快地使用crud文件)

<?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();
    }
}

1 个答案:

答案 0 :(得分:0)

我在Pages wont load if it has session_start(); in it找到了答案。

问题出在xampp和php 7,我将其更改为wamp,它工作正常。

希望它对某人有帮助!