Showing posts with label URI. Show all posts
Showing posts with label URI. Show all posts

Monday, June 26, 2017

Validating a URL in PHP

Check if the variable $url is a valid URL:

The FILTER_VALIDATE_URL filter validates a URL.

Possible  flags:

FILTER_FLAG_SCHEME_REQUIRED - URL must be RFC compliant.
Like http://example

FILTER_FLAG_HOST_REQUIRED - URL must include host name.
Like http://www.example.com

FILTER_FLAG_PATH_REQUIRED - URL must have a path after domain.
Like www.example.com/example1/

FILTER_FLAG_QUERY_REQUIRED - URL must have a query string.
Like "example.php?name=Peter&age=37"

filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED)
filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)
filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)
filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED)


Friday, April 21, 2017

How to get complete current URL for Cakephp 3.x

Below are few steps described to get URL inside Cakephp 3.x controller as well as view file. You can get full URL and base URL and current URL inside Controller.

The very first thing is to include Router in your Controller as below:

use Cake\Routing\Router;


public function urls()
{
    echo "<div style=''>";
    echo "Current URL=" . Router::url(null, true);
    echo "<BR>Current URL=" . Router::url(null, false);
    echo "<BR>Current URL=" . $this->request->getUri();
    echo "<BR>Current URL=" . $this->request->getUri()->getPath();
    echo "<BR>Current URL=" . $this->request->getRequestTarget();
    echo "<BR>Full URL=" . Router::url("/", true);
    echo "<BR>Base URL=" . Router::url("/", false);
    die("</div>");
}


And output would like this:

Current URL=http://localhost/cake/pages/urls
Current URL=/cake/pages/urls
Current URL=http://localhost/pages/urls?id=20&name=Pritom
Current URL=/pages/urls
Current URL=/pages/urls?id=20&name=Pritom
Full URL=http://localhost/cake/
Base URL=/cake/

Wednesday, September 11, 2013

HTTP RAW URL Address Encode in Java as application/x-www-form-urlencoded


package com.pritom.kumar;

import java.net.URLDecoder;
import java.net.URLEncoder;

public class RawUrlEncodeDecode {
    public static void main(String[] args) throws Exception{
        String toEncode = "a & b+c_d+e / é <p>Hello</p><?xml version='1.0'><name>Pritom K Mondal</name>";
        String encoded = URLEncoder.encode(toEncode, "UTF-8");
        System.out.println("Encoded: " + encoded);
        String decoded = URLDecoder.decode(encoded, "UTF-8");
        System.out.println("Decoded: " + decoded);
    }
}

Output

Encoded: a+%26+b%2Bc_d%2Be+%2F+%C3%A9+%3Cp%3EHello%3C%2Fp%3E%3C%3Fxml+version%3D%271.0%27%3E%3Cname%3EPritom+K+Mondal%3C%2Fname%3E

Decoded: a & b+c_d+e / é <p>Hello</p><?xml version='1.0'><name>Pritom K Mondal</name>