2

In my website I have one form in that have one URL input field, so I want to check when ever the user entered url like the following combination http://example.com or www.example.com or example.com. But i want to store in my database as http://www.example.com. How can i do that using PHP not using .htaccess or jquery or javascript validation.

// Check, if not have http:// or https:// then prepend it
if (!preg_match('#^http(s)?://#', $url))
{
$url = 'http://' . $url;
} 

and the following code checked url validation

if (filter_var($source_url, FILTER_VALIDATE_URL) === FALSE)
{
    echo "<p class='error'>Oops! You Forgot to Enter Website URL</p>";
}
Udhayakumar
  • 261
  • 1
  • 10
  • 3
    I do not recommend you to do that. Sometimes `www.mysite.com` and `mysite.com` is not the same thing. – sobolevn Jun 08 '15 at 17:41
  • 2
    Not sure why .htaccess is not an option, it's the best way to do this. The way you're currently coding it could pose issues. That being said, you'll probably want to use PHP Headers. 301 Redirect the non-www pages to www pages. Best of luck. – NotJay Jun 08 '15 at 17:44

3 Answers3

1

Following this page and the code posted by thomas there:

<?php 

$url = 'http://usr:pss@example.com:81/mypath/myfile.html?a=b&b[]=2&b[]=3#myfragment'; 
print append_www_to_host($url); 

function append_www_to_host($url) {
  $parsed_url = url_parse($url); 
  $scheme   = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : 'http://'; 
  $host     = isset($parsed_url['host']) ? startsWith($parsed_url['host'], 'www.') ? $parsed_url['host'] : 'www.' . $parsed_url['host'] : ''; 
  $port     = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; 
  $user     = isset($parsed_url['user']) ? $parsed_url['user'] : ''; 
  $pass     = isset($parsed_url['pass']) ? ':' . $parsed_url['pass']  : ''; 
  $pass     = ($user || $pass) ? "$pass@" : ''; 
  $path     = isset($parsed_url['path']) ? $parsed_url['path'] : ''; 
  $query    = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : ''; 
  $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : ''; 
  return "$scheme$user$pass$host$port$path$query$fragment"; 
} 

function startsWith($haystack, $needle) {
    // search backwards starting from haystack length characters from the end
    return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
}

?>

Note the line that starts with $host, which is where the www. is being prepended. This should print out http://usr:pss@www.example.com:81/mypath/myfile.html?a=b&b[]=2&b[]=3#myfragment.

That said, I think this is a bad idea for many reasons - you may be invalidating the URL (for example, what if your user entered m.facebook.com, and now you're storing www.m.facebook.com, which doesn't exist?).

Dan Field
  • 20,885
  • 5
  • 55
  • 71
0

Try parse_url: http://php.net/manual/en/function.parse-url.php

<?php
$parsed_url = parse_url( $url );
if( $parsed_url == false)
{
    // $url is invalid
}

if( $parsed_url['scheme'] != 'http' )
{
    $url = 'http://' . $url;
}

However, there are issues with this - what if the site is https? Or the site is on a subdomain other than www?

Might be better to check if the URL exists by attempting to load it via a file command or Curl.

See: How can I check if a URL exists via PHP?

Community
  • 1
  • 1
theHands
  • 373
  • 1
  • 3
  • 8
0

What you need is to adding www if it not exists:

$uri = "http://google.com";
if (preg_match("/http:\/\/(w){3}./" , $uri)) {
    echo "It matches...";
} else {
    echo "not match, try to add";
    if (substr($uri , 0,5) =='https') $uri = str_replace("https://" , "https://www." , $uri); else $uri = str_replace("http://" , "http://www." , $uri); 
    echo $uri;
}
Alex
  • 715
  • 5
  • 14