rinru
rinru

Reputation: 141

Codeigniter url redirect

I used to encrypt the id of my data in my url ex. mywebsite.com/product/detail/encryptedid then if i would go to that link it will show all the info of that specific product. but when i tried to change the encryptedid such as some random string ex. mywebsite.com/product/detail/asd123 it will show the error "Trying to get property of non-object" I'm curious how to prevent it in such a way like if they will change the encrypted url to some random strings it can redirect to the original mywebsite.com/product/detail/encryptedid which will show still the info of that specific product or go to 404 page.

for clear example look at the image below.

Image with encryption

with encrypt id

Image with inputted random string

without encrypt id

EDITED:

My Controller

public function details($id){

    $decrypt = secret_url('decrypt',$id);

    $prod_row = $this->ProductModel->getonerow($decrypt);
    $data['product_detail'] = $prod_row;

    $data['title'] = "Ayos Computer | Product Details";
    $data['category'] = $this->ProductModel->get_category();
    $data['products'] = $this->ProductModel->get_product();

    $this->load->view('include/header',$data);
    $this->load->view('include/nav');
    $this->load->view('products/ProductDetail', $data);
    $this->load->view('include/footer');
}

VIEW

 <a href="<?= base_url() .'products/details/' . secret_url('encrypt',$featured_row->product_id) ?>"></a>

thank you in advance.

Upvotes: 0

Views: 87

Answers (1)

Tpojka
Tpojka

Reputation: 7111

As same way as if there was no product:

public function details($id)
{

    $decrypt = secret_url('decrypt',$id);

    $prod_row = $this->ProductModel->getonerow($decrypt);

    if (null !== $prod_row) {
        $data['product_detail'] = $prod_row;

        $data['title'] = "Ayos Computer | Product Details";
        $data['category'] = $this->ProductModel->get_category();
        $data['products'] = $this->ProductModel->get_product();

        $this->load->view('include/header',$data);
        $this->load->view('include/nav');
        $this->load->view('products/ProductDetail', $data);
        $this->load->view('include/footer');
    } else {
        // CI default 404 view if left blank value
        show_404($custom_page = '');
    }
}

Upvotes: 1

Related Questions