php - inspect data (url variable) before inserting to mysql -
My PHP script is posting bad URLs that AJAX sends it. When I try to filter the url and put it in mysql, it does not work. What I want to do is verify this $ link variable if it is a link if not, do not post the data to mysql. Do I know what I did wrong and how did it fix it? Thanks :) Here's my code
$ con = mysql_connect ("localhost", "root", ""); If (! $ Con) {dead ('Could not connect:' Mysql_error ()); } Mysql_select_db ("database", $ conn); $ Link = $ _POST ['New']; $ Name = $ _POST ['name']; $ Size = $ _POST ['size']; $ Cat = $ _POST ['Cat']; // PHP 5.3.5-1ubuntu7.2 $ link = mysql_real_escape_string ($ link); $ Name = mysql_real_escape_string ($ name); $ Size = mysql_real_escape_string ($ size); $ Cat = mysql_real_escape_string ($ Cat); If (filter_var ($ link, FILTER_VALIDATE_URL)) {} else {echo "URL is not valid"; Mysql_close ($ thief); Go out(); } $ Check = mysql_query ("Select link from link where link = '{$ link}';"); Enter the value ('$ link', '$ name', '$ size', '$ (if you enter mysql_num_rows ($ check) == 0) {// $ sql = "links (link, name, size, category) cat')"; If (! Mysql_query ($ sql, $ con)) {dead ('error:' mysql_error ()); } Echo "1 record added. Redirect!"; Mysql_close ($ thief); }
Try this:
Filter_var ($ link, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED) === incorrect) {echo "URL is not valid"; Mysql_close ($ thief); Go out(); } Your correct xxxhxtxtp: //example.com will be passed as a valid URL. Instead of preg_match . if (preg_match ("# ^ http (s) ?: // [a-z0-9-_.] + \. [Az] {2, 4} # u" , $ Link)) {echo "url is not valid"; Mysql_close ($ thief); Go out(); } With this regex you need the http (s) plan. So if you do not work for you then you can search for other Reggae.
Comments
Post a Comment