Jump to content

Recommended Posts

Hi,

 

I'm trying to create a function which checks if a url has been posted before,and if it has then an error is given.

 

This is what I have so far in /includes/functions_posting.php

 

    function http_file_exists($url)
    {
      $f = @fopen($url,"r");
      if($f)
      {
        fclose($f);
      return true;
      }
    return false;
    } 

 

And this is what I have in posting.php

    if ($submit || $preview || $refresh)
    {
     $post_data['your_url'] = "http://www.google.com"; //remove the equals and url value if using in real post
      $your_url = $post_data['your_url'];
      $your_url_exists = (isset($your_url)) ? true : false;
      $your_url = preg_replace(array('#&\#46;#','#&\#58;#','/\[(.*?)\]/'), array('.',':',''), $your_url);

      if ($your_url_exists && http_file_exists($your_url) == true)
      {
        trigger_error('exists!');
      }
      else if ($your_url_exists && http_file_exists($your_url) == false)
      {
        trigger_error('doesnt exist..');
      }

 

It works fine,but I still don't know how to do one last thing.

 

if the url doesn't exist,I want it to continue posting. else,trigger the error message that url exists.

 

I think I'm close,but not sure where to go now  :confused:

 

Thanks..

Link to comment
https://forums.phpfreaks.com/topic/255626-check-if-link-in-post/
Share on other sites

Change...

if ($your_url_exists && http_file_exists($your_url) == true)
      {
        trigger_error('exists!');
      }
      else if ($your_url_exists && http_file_exists($your_url) == false)
      {
        trigger_error('doesnt exist..');
      }

 

To...

if ($your_url_exists && http_file_exists($your_url) == true)
      {
        trigger_error('exists!');
      }
      else
      {
        // whatever code you have to continue posting
      }

 

Also, I would rewrite your function like this...

function http_file_exists($url)
{
   if (file_exists($url))
   {
      return true;
   }
   return false;
} 

Actually, the suggestion I made wasn't well thought out. Instead, do this:

 

if ($your_url_exists && file_exists($your_url) == true)
      {
        trigger_error('exists!');
      }
else
      {
        // continue posting
      }

 

Don't use your function because there already is a function that does what you need; i.e., it's redundant.

If you're testing a true/false condition, you really don't need an else if statement, because either it's true or it's not, so you just need an else block.

 

Basically, the only time you would want an if...else if is if you're testing for multiple conditions.

 

if condition A // then whatever for condition A
else if condition B // then whatever for condition B
else if condition C // then whatever for condition C
else // default

 

Since you're only testing for true/false, you just need an if...else.

 

if true // then it's true
else // it's obviously false

 

what do I write inside the else if condition ?

 

Whatever you want. I don't know how your application is supposed to work. This part is your job to figure out.

lol again,I understand the first part. I'm not totally new to PHP.

 

What I don't know is how my application is suppose to work if the result is false,and how do I change the way it works if the result is false. if I knew that,I wouldn't create the topic.

 

Thanks.

What I don't know is how my application is suppose to work if the result is false,and how do I change the way it works if the result is false.

if the url doesn't exist,I want it to continue posting. else,trigger the error message that url exists.

 

if ($your_url_exists && file_exists($your_url) == true)
// DO NOT POST
else // CONTINUE POSTING

 

However you decide to implement the posting of the URL, you would put it in that ELSE block. Just because the condition tests false doesn't mean your application stops working.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.