wadie Posted January 23, 2012 Share Posted January 23, 2012 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 Thanks.. Quote Link to comment https://forums.phpfreaks.com/topic/255626-check-if-link-in-post/ Share on other sites More sharing options...
Proletarian Posted January 23, 2012 Share Posted January 23, 2012 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; } Quote Link to comment https://forums.phpfreaks.com/topic/255626-check-if-link-in-post/#findComment-1310467 Share on other sites More sharing options...
Proletarian Posted January 24, 2012 Share Posted January 24, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255626-check-if-link-in-post/#findComment-1310552 Share on other sites More sharing options...
wadie Posted January 24, 2012 Author Share Posted January 24, 2012 That's not what I really want. I want instead the else if,something to let the post get posted. so the part the I need even if there still should be an else if is,what do I write inside the else if condition ? Quote Link to comment https://forums.phpfreaks.com/topic/255626-check-if-link-in-post/#findComment-1310563 Share on other sites More sharing options...
Proletarian Posted January 24, 2012 Share Posted January 24, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255626-check-if-link-in-post/#findComment-1310574 Share on other sites More sharing options...
wadie Posted January 24, 2012 Author Share Posted January 24, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255626-check-if-link-in-post/#findComment-1310576 Share on other sites More sharing options...
Proletarian Posted January 24, 2012 Share Posted January 24, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255626-check-if-link-in-post/#findComment-1310577 Share on other sites More sharing options...
wadie Posted January 24, 2012 Author Share Posted January 24, 2012 ok ok I'll see what I can do.. More suggestions are welcome! Quote Link to comment https://forums.phpfreaks.com/topic/255626-check-if-link-in-post/#findComment-1310599 Share on other sites More sharing options...
wadie Posted January 25, 2012 Author Share Posted January 25, 2012 OK I have what I want. I just removed the else if part and it's posting if the link doesn't exist. Problem: How do I make it check if the domain.com and the rest of it for example domain.com/something exists ? Quote Link to comment https://forums.phpfreaks.com/topic/255626-check-if-link-in-post/#findComment-1311088 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.