htmlstig Posted January 1, 2009 Share Posted January 1, 2009 hi i have worte a simple script that has a form and uploads the data from the from to a mysql database upon submit. one of my fields is a url field, how would i check to see if the user has entered http:// before the address and if its not there add it to the address before sending it to mysql? Cheers Carl Quote Link to comment Share on other sites More sharing options...
corbin Posted January 1, 2009 Share Posted January 1, 2009 if(stripos($url, 'http://') === 0) { //starts with http:// } You could also use regular expressions, but there isn't a very good advantage to that. If you wanted to check for https:// you could just use an ||. Quote Link to comment Share on other sites More sharing options...
htmlstig Posted January 1, 2009 Author Share Posted January 1, 2009 sorry if this sounds daft ... does that add the http:// to the address aswel or does it just check to see if its there? Quote Link to comment Share on other sites More sharing options...
opalelement Posted January 1, 2009 Share Posted January 1, 2009 sorry if this sounds daft ... does that add the http:// to the address aswel or does it just check to see if its there? if(stripos($url, 'http://') == 0) { //starts with http:// } else { $url = "http://".$url; } or to just append it: if(stripos($url, 'http://') != 0) { $url = "http://".$url; } Quote Link to comment Share on other sites More sharing options...
htmlstig Posted January 1, 2009 Author Share Posted January 1, 2009 for some reason i couldnt get those to work this is my code <? if (isset($_POST['submit'])){ $name=$_POST['name']; $email=$_POST['email']; $website=$_POST['website']; $comment=$_POST['comment']; } if(stripos($website, 'http://') == 0) { } else { $website = "http://".$website; } mysql_connect($server, $username, $password) or die(mysql_error()); mysql_select_db($dbname) or die(mysql_error()); mysql_query("INSERT INTO `guestbook` VALUES ( '', NOW(), NOW(), '$name', '$email', '$website', '$comment')"); header("location: $_SERVER[php_SELF]"); } ?> Quote Link to comment Share on other sites More sharing options...
opalelement Posted January 1, 2009 Share Posted January 1, 2009 Any errors? Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted January 1, 2009 Share Posted January 1, 2009 Watch the number of = characters if(stripos($website, 'http://') === 0) { } else { $website = "http://".$website; } Quote Link to comment Share on other sites More sharing options...
htmlstig Posted January 1, 2009 Author Share Posted January 1, 2009 thank you that sorted it i didnt notice the difference between the 2 posts Quote Link to comment Share on other sites More sharing options...
opalelement Posted January 2, 2009 Share Posted January 2, 2009 My bad, forgot that last = Quote Link to comment 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.