E
E
Elsa122018-10-17 09:14:01
PHP
Elsa12, 2018-10-17 09:14:01

How to validate URL in php?

Good afternoon! I have a Python server running on flask, it looks pretty primitive

import subprocess
import sys,validators, base64
from flask import Flask
from flask import Response
import urlparse

def get_domain(url):
  base_url = urlparse.urljoin(url, '/') #"{0.scheme}://{0.netloc}/".format(urlsplit(url))
  base_url = base_url[(base_url.index('://')+3):-1]
  if base_url[:4] == "www.":
      base_url = base_url[4:]
  return base_url
app = Flask(__name__)
@app.route("/<url>", methods=['GET'])
def index(url):
    url = base64.b64decode(url)
    if url.find("http") < 0:
        url = "http://" + url
    if validators.url(url) == True: 
        print("Valid")
    else:
        # Check valid adress, do error heandler
        url = "https://sample.com"
    domain = get_domain(url)
    print(url)
    print(domain)
    cmd = './my_app ' + base64.b64encode(url) + ' ' + base64.b64encode(domain)
    PIPE = subprocess.PIPE
    p = subprocess.Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE,
            stderr=subprocess.STDOUT, close_fds=True, cwd='/home/dir')
    resp = Response(p.stdout.read())
    resp.headers['Access-Control-Allow-Origin'] = '*'
    return resp
if __name__ == "__main__":
    context = ("/var/www/httpd-cert/www-root/u_le1.crt", "/var/www/httpd-cert/www-root/u_le1.key")
    app.run(host='0.0.0.0', port=9090, ssl_context=context)

How to implement the same only in php? The whole difficulty is in the validation of domains ..

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
KazeZlat, 2018-10-17
@KazeZlat

If you ask Google " validate url php ", then somewhere at the beginning of the output, the documentation for the filter_var() function will be released , and you can use it to reach the list of filters .
And then, if the URL is valid (taking into account all the necessary flags that you could set), you can parse the URL using parse_url ()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question