我对codeiginter真的很陌生。我正在尝试创建一个博客站点。在那里,我创建了一个评论博客帖子的功能。在评论之前,用户需要登录该站点。因此,如果未登录的用户尝试发表评论,我想将其重定向到登录页面。我使用重定向功能将用户重定向到登录页面。
我的代码是: (控制器)
public function add_review($pots_id)
{
$form_data = $this->input->post();
$this->load->model("blog_model");
if(isset($form_data['comment']))
{
$state = $this->isBad($form_data['comment']);
}
else
{
$state = $this->isBad("");
}
$this->data['blog_comment_back'] = -1;
if(isset($_SESSION['Username']))
{
if(strcmp($state, "-1")==0)
{
if(strcmp(substr($pots_id,0,3), "Art")==0)
{
$status = $this->blog_model->add_review($pots_id, $form_data,$_SESSION['Username']);
}
}
else
{
$msg = "Your comment has inappropriate word. Please correct them";
$this->data['blog_comment_back'] = $state;
echo "<script>alert('Please check your comments. You have got inappropriate words in it.');</script>";
}
}
else{
echo "<script>alert('Please login to comment.');</script>";
$this->session->set_flashdata('redirectToCurrent', $form_data);
$this->session->set_flashdata('crurrentData', current_url());
redirect(base_url().'welcome/login','refresh');
// var_dump($this->session->flashdata('crurrentData'));
// var_dump($this->session->flashdata('redirectToCurrent'));
}
$this->data['blog_s'] = $this->blog_model->blog_singel_load($pots_id); // Get the article details
$this->data['blog_review'] = $this->blog_model->get_review($pots_id);
$this->load->view('blog-single',$this->data);
}
这是我的blog_model.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Blog_model extends CI_Model
{
public function load_blog_details() {
$this->db->select("*");
$this->db->from('Articles');
$query = $this->db->get();
return $query->result();
}
public function blog_singel_load($post_id) {
$this->db->select("*");
$this->db->from('Articles');
$this->db->where('Article_ID',$post_id);
$query = $this->db->get();
return $query->result();
}
public function get_review($post_id)
{
$this->db->select("*");
$this->db->from('Articel_review');
$this->db->where('Articel_ID',$post_id);
$query = $this->db->get();
return $query->result();
}
public function add_review($id,$form_data, $uname) {
$data = array(
'Articel_ID' => $id,
'user_name' => $uname,
'comment' => $form_data['comment']
);
//var_dump($this->db->insert('Articel_review', $data));
return $this->db->insert('Articel_review', $data);
}
}