CodeIgniter Forums
How can insert this into the databse after checking with fopen - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How can insert this into the databse after checking with fopen (/showthread.php?tid=11523)



How can insert this into the databse after checking with fopen - El Forum - 09-11-2008

[eluser]phantom-a[/eluser]
This is controller that handles a form submission with one field. a field called "url"
I use fopen to check if the url the person submitted is valid, then I want insert it into a table called "links" into a field called "submitted_url".

How can this be done? I have already the function working except missing the databse part where it inserts it into the table if its a valid url.

Code:
function submit()
    {
        // Get the submit url
        $submission_url = $this->input->post('url');
        //check if it a real url
        $check_url = @fopen($submission_url,'r');
        @fclose($check_url); //close it now
//was the url valid?
        if($check_url !== false){
        $data['message'] = "Success!";
    //missing function here to insert $submission_url into database    
        $this->load->view('message_form', $data);
    }else{
    //nice try
      $data['message'] = "That Url doesn't exist!";
      $this->load->view('message_form', $data);
     }
  }



How can insert this into the databse after checking with fopen - El Forum - 09-11-2008

[eluser]Sumon[/eluser]
Use a model
Code:
class url_model extends Model{
function insert_links($url)
{
  $data = array('submitted_url' => $url);
  $this->db->insert('links', $data);
  return true;
}
}

and use this model in your controller

Code:
function submit()
    {
        // Get the submit url
        $submission_url = $this->input->post('url');
        //check if it a real url
        $check_url = @fopen($submission_url,'r');
        @fclose($check_url); //close it now
//was the url valid?
        if($check_url !== false){
        $data['message'] = "Success!";
        $this->load->model('url_model');
        $this->url_model->insert_links($submission_url);
        $this->load->view('message_form', $data);
    }else{
    //nice try
      $data['message'] = "That Url doesn't exist!";
      $this->load->view('message_form', $data);
     }
  }



How can insert this into the databse after checking with fopen - El Forum - 09-11-2008

[eluser]phantom-a[/eluser]
That works! Thanks for example.


How can insert this into the databse after checking with fopen - El Forum - 09-11-2008

[eluser]phantom-a[/eluser]
eh just a thought is there security risks with fopen? I know "r" on it means readonly, but still What if someone submitted

http://example.com/trojan.exe

would my server download a virus?