ukweb Posted September 12, 2008 Share Posted September 12, 2008 As part of a CMS I'm currently building for a client, the system allows for the user to add links to their site. Basically, the link submitted from the form must start with a valid url start thingy, ie http://. I'm not sure how I would get php to check the submitted variable for this. See below if I;m not making sense here! <?php $url_types = array('http://', 'https://', 'ftp://'); $url = $_POST['url']; // If ( $url starts with $array_types ) { // Add to database } else { $error = $error."Your link must start with 'http://', 'https://' or 'ftp://'.<br />"; } Hope this makes sense! Thanks for any light that can be shed here! Stephen Quote Link to comment https://forums.phpfreaks.com/topic/123919-solved-if-variable-contains-http/ Share on other sites More sharing options...
aebstract Posted September 12, 2008 Share Posted September 12, 2008 if (eregi ($url, ^(http://)|(https://)|(ftp://)) { true } else { false } something like this should work Quote Link to comment https://forums.phpfreaks.com/topic/123919-solved-if-variable-contains-http/#findComment-639693 Share on other sites More sharing options...
aebstract Posted September 12, 2008 Share Posted September 12, 2008 ps: forgot a ) at the end of line 1 Quote Link to comment https://forums.phpfreaks.com/topic/123919-solved-if-variable-contains-http/#findComment-639699 Share on other sites More sharing options...
ukweb Posted September 12, 2008 Author Share Posted September 12, 2008 Parse error: syntax error, unexpected '^' in /Library/WebServer/Documents/allturf.co.uk/admin/core.php on line 31 is what I got, no luck yet! thanks for the reply though... Quote Link to comment https://forums.phpfreaks.com/topic/123919-solved-if-variable-contains-http/#findComment-639731 Share on other sites More sharing options...
ukweb Posted September 12, 2008 Author Share Posted September 12, 2008 Here's the full script in its entirity for this section of the back end: // Add Link to Database if ($_POST['a'] == 'add_link') { $url = $_POST['url']; $title = $_POST['title']; $uni_id = $_POST['uni_id']; $target = $_POST['target']; if ($title == '') { $error = $error.'A link title was not provided.<br />'; } else { ++$trigger; } if ($url == '') { $error = $error.'A link url was not provided.<br />'; } else if (eregi ($url, ^('http://') | ('https://') | ('ftp://'))) { ++$trigger; } else { $error = $error.'An invalid URL was posted.<br />'; } if ($trigger == 2) { $insertSQL = sprintf("INSERT INTO links (uni_id, title, target, url) VALUES (%s, %s, %s, %s)", GetSQLValueString($uni_id, "text"), GetSQLValueString($title, "text"), GetSQLValueString($target, "text"), GetSQLValueString($url, "text")); mysql_select_db($database_sql, $sql); $result = mysql_query($insertSQL, $sql) or die(mysql_error()); if ($result) { $success = $success.'Link added successfully.<br/>'; } } } Quote Link to comment https://forums.phpfreaks.com/topic/123919-solved-if-variable-contains-http/#findComment-639735 Share on other sites More sharing options...
Adam Posted September 12, 2008 Share Posted September 12, 2008 Not amazing with regular expressions but could try adding.. eregi ($url, \^('http://') | ('https://') | ('ftp://')\) or... eregi ($url, "^('http://') | ('https://') | ('ftp://')") Adam Quote Link to comment https://forums.phpfreaks.com/topic/123919-solved-if-variable-contains-http/#findComment-639743 Share on other sites More sharing options...
rhodesa Posted September 12, 2008 Share Posted September 12, 2008 also can try: <?php if(!parse_url($url)) die("Invalide URL"); ?> ...or to restrict to http, https, ftp: <?php $allowed_scheme = array('http','https','ftp'); if(!($parts = parse_url($url))) die("Invalide URL"); if(!in_array($parts['scheme'],$allowed_scheme)) die("Invalid scheme"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/123919-solved-if-variable-contains-http/#findComment-639745 Share on other sites More sharing options...
ukweb Posted September 13, 2008 Author Share Posted September 13, 2008 Thanks everyone, the final solution is what worked for me. Quote Link to comment https://forums.phpfreaks.com/topic/123919-solved-if-variable-contains-http/#findComment-640436 Share on other sites More sharing options...
DarkWater Posted September 13, 2008 Share Posted September 13, 2008 By the way aebstract, you were missing ' ' around your string. Also, don't use ereg(). Quote Link to comment https://forums.phpfreaks.com/topic/123919-solved-if-variable-contains-http/#findComment-640445 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.