2

Using this form, users can find out all the urls of a website. It works well if the user submits a url. But if they submit a domain name, it doesn't work.

<?
if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    $url= $_POST["url"];

    $data=file_get_contents($url);
    $data = strip_tags($data,"<a>");
    $d = preg_split("/<\/a>/",$data);
    $count=1;
    foreach ( $d as $k=>$u ){
        if( strpos($u, "<a href=") !== FALSE ){
            $u = preg_replace("/.*<a\s+href=\"/sm","",$u);
            $u = preg_replace("/\".*/","",$u);
            echo "<hr>".$count++.$u."\n";
        }
    }
}
?>

<html>
<body>
<form style="margin: 10px;" action="" method="post">
        <label for="url">Enter a URL</label>
        <input type="text" name="url" id="url">
        <input type="submit" value="Go">
    </form>
</body>
</html>
4
  • 1
    use .htaccess for that Commented Dec 12, 2016 at 16:11
  • Could you please explain it? Commented Dec 12, 2016 at 16:13
  • below is the answer Commented Dec 12, 2016 at 17:07
  • 2
    Welcome to Stackoverflow, I've looked at your profile, you have 10 questions without a single accepted answer (most of the questions got answers in them, I take an assumption that at least one solved a question). In here you have 6 answers which will solve your problem. I would recommend start accepting answers (if it had solved your issue of course), both for the community and for you, as low acceptance rate may cause your future questions not to be answered. Commented Dec 12, 2016 at 19:59

6 Answers 6

1

You can use regex to solve your issue:

$url = "google.com";

$formatted_url = addHttpToUrl($url);

function addHttpToUrl($url) {
    if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
        $url = "http://" . $url;
    }
    return $url;
}

// $formatted_url = "http://google.com"

Hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

@ajesh kdy - If you find this answer correct and helpful then accept & upvote this answer as it motivates me to give answers to other questions like this and helps others to quickly find the correct answer!
1

You can use the following function to check if a string is starting with another string:

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

then, if it doesnt start with http:// concatenate it as a prefix.

if(!startsWith(strtolower($url), "http://"))
{
    $url = 'http://'.$url;
}

Comments

1

this would be an example:

if ( strpos($url, 'http://') === false || strpos($url, 'https://') === false ) {
    $url = 'http://'.$url;
}

Comments

0
if (!preg_match('#^https?://#i', $url)) {
  $url = 'http://'.$url;
}

1 Comment

While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please include an explanation for your code, as that really helps to improve the quality of your post. Flaggers / reviewers: For code-only answers such as this one, downvote, don't delete! (Note: This answer may actually be simple enough to make an explanation, and thus downvotes, unnecessary. You may still want to add an explanation to prevent more NAA/VLQ flags.)
0

you asked for an example with .htaccess here it is:

RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Comments

0

Use filter_var() to check if the user input is already a valid URL.

Right after $url= $_POST["url"];, add the following code:

if(!filter_var($url,FILTER_VALIDATE_URL)) $url = 'http://'.$url;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.