如何为注册和登录脚本设置co​​okie

时间:2015-09-22 11:29:29

标签: php cookies

我在设置Cookie时遇到问题。我有一个注册和登录表单,用户被重定向到另一个页面,其中包括register.php,这将向登录的用户和访客访问者(未登录)显示不同的数据。但注册后,登录标题菜单仍然会注册并登录

<?php $con = mysqli_connect("localhost","root","","findfriends") or die ("Connection not established"); ?> <?php if(isset($_POST['reg'])){ $fn=strip_tags(@$_POST['fname']); $ln=strip_tags(@$_POST['lname']); $un=strip_tags(@$_POST['username']); $em=strip_tags(@$_POST['email']); $pswd=strip_tags(@$_POST['password']); $d= date("Y-m-d"); $reg_query = "INSERT INTO users (userid,username,first_name,last_name,email,password,sign_up_date,activated,bio,profile_photo,closed) VALUES ('','{$un}','{$fn}','{$ln}','{$em}','{$pswd}','{$d}','0','What you do?','','no')"; $reg_run = mysqli_query($con,$reg_query); setcookie('user',$un,time()+3600*24*365); echo "The cookie has been set."; header("Location:main.php"); if(isset($_POST['login'])){ $log_que = "SELECT * FROM users WHERE username = '$un' AND password = '$pswd' "; $log_run = mysqli_query($con, $log_que); $row = mysqli_fetch_array($log_run); $un_db = $log_row['username']; $pswd_db = $log_row['password']; if($un == $un_db && $pswd == $pswd_db) { echo "LOGGED IN!"; setcookie('user',$un,time()+3600*24*365); header("Location: main.php"); } } ?>

header.php

<!DOCTYPE html> <html> <head> <title>findfriends</title> <script src="js/main.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="./css/style.css"/> </head> <body> <div class="headerMenu"> <div id="wrapper"> <div class="logo"> <img src="./img/logo.gif"/> </div> <div class="search_box"> <form action="searchresults.php" method="post" name="search"> <table> <tr> <td> <input type="text" name="search" placeholder="Search ..."/> </td> <td> <input type="image" src="./img/search-icon.png" alt="submit" /> </td> </tr> </table> </form> </div> <?php if(isset($_COOKIE['user'])){ echo ' <ul class="dd"> <li><a href="main.php" >Home</a> </li> <li><a href="' . $user . '">Profile</a> </li> <li><a href="my_messages.php">Inbox' . $unread_numrows . '</a> </li> <li><a href="#">Management</a> <ul><li><a href="account_settings.php">Settings</a> </li> <li><a href="logout.php">Logout</a> </li> </ul> </li> </ul>'; } else { echo ' <ul class="dd"> <li><a href="register.php" >Sign Up</a> </li> <li><a href="register.php">Login</a> </li> </ul>'; } ?> </div> </div> </body> </html>

class Mcrud extends CI_Model {

    public function record_count() {
        return $this->db->count_all("crud");
    }

    function view() {
    $ambil = $this->db->from('crud')->order_by('idcrud', 'DESC')->limit(5, 3)->get();
        if ($ambil->num_rows() > 0) {
            foreach ($ambil->result() as $data) {
                $hasil[] = $data;
            }
            $ambil->free_result();
            return $hasil;          
        }
    }

2 个答案:

答案 0 :(得分:0)

如果在标题功能之前输出,则需要设置输出缓冲区 在第一行添加

<?php ob_start(); ?>

最后

<?php ob_flush(); ?>

答案 1 :(得分:0)

您必须在login.php / login.html文件中添加if else语句  您网页的每个文件

if($user===not_logged_in){
 header("Location: login.php");
} else{
//do somthing else
}