How to Properly Create HTML Links in PHP

How to properly create HTML links in PHP?

How to dynamically build HTML links with query string?

If you need to create query string to be used in HTML link (e.g. <a href="index.php?param1='.$value.'">Link</a>) then you should use http_build_query.
This function accepts 4 parameters, with the first one being an array/object of query data. For the most part the other 3 parameters are irrelevant.

$qs = [
'a' => 'a',
'quot;' => 'bar foo',
];
echo '<a href="?' . http_build_query($qs) . '">Link</a>';

However, you should still pass the output of the function through htmlspecialchars to encode the & correctly. "A good framework will do this automatically, like Laravel's {{ }}"

echo '<a href="?' . htmlspecialchars(http_build_query($qs)) . '">Link</a>';

Alternatively you can pass the third argument to http_build_query as '&', leaving the second one null. This will use & instead of & which is what htmlspecialchars would do.

About spaces.

For use in form data (i.e. query strings) the space should be encoded as + and in any other place it should be encoded as %20 e.g. new%20page.php?my+field=my+val. This is to ensure backwards comparability with all browsers. You can use the newer RFC3986 which will encode the spaces as %20 and it will work in all common browsers as well as be up to date with modern standards.

echo '<a href="?' . http_build_query($qs, null, '&', PHP_QUERY_RFC3986) . '">Link</a>';


rawurlencode vs urlencode

For any part of URL before ? you should use rawurlencode. For example:

$subdir = rawurlencode('blue+light blue');
echo '<a href="'.$subdir.'/index.php">rawurlencode</a>';

If in the above example you used urlencode the link would be broken. urlencode has very limited use and should be avoided.

Do not pass whole URL through rawurlencode. Separators / and other special characters in URL should not be encoded if they are to fulfil their function.


Footnote

There is no general agreement on the best practices for using http_build_query, other than the fact it should be passed through htmlspecialchars just like any other output in HTML context.

Laravel uses http_build_query($array, null, '&', PHP_QUERY_RFC3986)

CodeIgniter uses http_build_query($query)

Symfony uses http_build_query($extra, '', '&', PHP_QUERY_RFC3986)

Slim uses http_build_query($queryParams)

CakePHP uses http_build_query($query)

Twig uses http_build_query($url, '', '&', PHP_QUERY_RFC3986)

How to create a link to another PHP page

Use like this

<a href="index.php">Index Page</a>
<a href="page2.php">Page 2</a>

PHP to create Links for files in a folder

You can add this function to help with getting the dates from the filenames

function getDateFromFileName($filename){
$dateObj = DateTime::createFromFormat('m-Y',substr($filename,1,2).'-'.substr($filename,3,6));
return $dateObj->format('F Y');
}

Calling getDateFromFileName("n102017.pdf") prints October 2017

EDIT:

To change the name in your scenario do

<?php
$dir = "var/newsletters/";

function getDateFromFileName($filename){
$dateObj = DateTime::createFromFormat('m-Y',substr($filename,1,2).'-'.substr($filename,3,6));
return $dateObj->format('F Y');
}
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
if($file == "." || $file ==".."){
continue;
}
$formatted_date = getDateFromFileName($file);
echo " <a href=var/newsletters/$file>{$formatted_date}</a><br>";
closedir($dh);
}
}
?>

Creating links in PHP

Your HTML is messed up, that could be the problem, try putting '' suround the values of your html tags, like this:

<a href='viewgallery.php?cname=$cname&pcaption=".str_replace(" ","_",$pid_array[$prev])."'><img src='photos/assets/right.png' border='0'></a>

php includes that correctly links href from wherever

Finally worked it out. Mamp was confusing things and i had to include php in my link.

under MAMP>conf>apache>httpd.conf I changed my DocumentRoot to "/Applications/MAMP/htdocs/radventures.co.uk" so it pointed to the root of my site.

Changeable document_root for mamp configuration:

<?PHP
if ($_SERVER['DOCUMENT_ROOT'] == '/Applications/MAMP/htdocs/radventures.co.uk'){
$root = 'HTTP_ROOT';
}
else {
$root = 'DOCUMENT_ROOT';
}
$link = "$_SERVER[$root]";
?>

img link:

<img src="<?PHP echo ($link . "/assets/icons/facebook_1.png"); ?>

include:

<?PHP include ($_SERVER['DOCUMENT_ROOT'] . "/includes/footer.php"); ?>


Related Topics



Leave a reply



Submit