Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

Will Bontrager Blogs Website Techniques

WillMasterBlog > Tips and Tricks

Multiple Launches With One Cron Schedule

When you have several scripts that need to run at a specific frequency — a daily run of a script to compile stats from a log file is one example — setting up one cron instead of many may save time and frustration.

What you do is install the script below. In a separate file, list the URLs of the scripts that shall be run. Set up cron to run the script every day (or other schedule). When the script below runs, it does an HTTP request to each URL in your list, one URL at a time.

Unlike cron, this system lets you run scripts at any public URL at any domain, whatever PHP or other scripts that a browser or bot can access. Cron requires scripts to be available on the same server where cron is set up.

Most hosting companies provide a dashboard method for you to set up cron for your script. If your hosting company does not, articles Cron and Using Cron have information you may be able to use (warning, highly technical).

Here is the source code. One customization is required, talked about further below.

<?php
/*
Run All URLs
Version 1.0
March 17, 2024
Will Bontrager Software LLC
https://www.willmaster.com/
*/

// Specify the location of the file with the URLs to run.
$FileWithURLs = 'subdirectory/urls2run.txt';
// End of customizations.

$listOfURLs = array();
if( file_exists($FileWithURLs) ) { $listOfURLs = file($FileWithURLs); }
else { echo "File $FileWithURLs not found."; }
foreach( $listOfURLs as $url)
{
   $url=trim($url);
   if( preg_match('!^https?://!i',$url) )
      { file_get_contents($url); }
}
echo 'OK';
?>

Customization —

The above script will look for a text file (see next paragraph) that contains URLs to run. The text file can be anywhere on the server that could contain web pages.

When you have decided where to put the text file and what to name it, then replace subdirectory/urls2run.txt with the file's location. The file is not required to have a .txt file name extension; it can have any extension you wish to give it or even no extension at all. In other words, it needs to be a text file but you can give it any file name you wish to give it.

Upload the above source code as script runURLs.php or other *.php file name. Make a note of its URL.

Put the URLs of scripts you want to run into the text file specified in runURLs.php (see further above). URLs in the text file should be one URL per line. Script runURLs.php will ignore any lines that do not begin with http:// or https://.

Note: Do not specify the URL of runURLs.php in the text file containing URLs to run. You would end up with an infinite loop.

Set up a cron schedule for runURLs.php. Whenever runURLs.php runs, it will run the URLs in your text file as HTTP requests.

This system can put any URL on the internet on a scheduled run. URLs can be added and removed by updating the text file.

(This content first appeared in Possibilities newsletter.)


WillMasterBlog > Tips and Tricks

Blocking Text Field Form Submission

A form with only one text field and the cursor in that field will submit when the Enter/Return key is tapped, even when the form has no submit button.

That's the way it used to be.

Yesterday, I found out that a form may submit even when the form has more than one text field. A client found the "bug".

I fixed it by telling the form not to submit unless it is done with JavaScript. That worked.

Here is an example. The form will not submit when Enter/Return is tapped within the text field. It will submit only when the button is tapped. Note: The form submits to example.com in a new browser tab.

A text input field:

Here is how it was done.

  1. Put an id value and onsubmit="return false" attribute into the form tag. Here is an example:

    <form id="MyForm" onsubmit="return false" method="post" action="https://example.com/page.php">
    

    The id="MyForm" id value will be used in the next step.

    The onsubmit="return false" attribute prevents the user from submitting the form with HTML methods. It can only submit with JavaScript.

  2. Changed the submit button into type="button" and added an onclick=… attribute. Here is an example:

    <input type="button" onclick="document.getElementById('MyForm').submit()" value="Pay Now">
    

    The type="button" attribute will react only with JavaScript.

    The onclick="document.getElementById('MyForm').submit()" attribute submits the form when the button is tapped. The submission is accomplished by using the form field's id="MyForm" id value and the JavaScript submit() function.

The element with an onclick=… attribute doesn't have to be a button. It could be an image or anything else that is clickable, within or outside the form area.

The above steps will prevent a form from submitting except when a button with an onclick=… attribute is tapped.

(This content first appeared in Possibilities newsletter.)


WillMasterBlog > CSS

Table Layout Trick

Generally, browsers determine the width of table columns by the contents within the columns.

You may have noticed table columns changing width while a table is being loaded. The browser determines a new width for a column as it encounters content to be displayed within that column.

But what if each column needs to be a specific width? In that case, do two things:

  1. Use the CSS table-layout:fixed; declaration within the table tag.

  2. In the first row of the table, specify the width of the columns.

Here is a table to illustrate. Each column is specified to be 100 pixels wide.

Blue White, Blue, and Green
Orange Red, Orange, and Yellow

Telling the browser that the table layout is fixed means it does not have to spend time calculating widths for the table past the first row. The page is then in position to render more quickly. And, you have control over the width of table columns.

Here is the source code for the above table.

<table border="1" cellpadding="0" cellspacing="0" 
       style="table-layout:fixed; border:1px solid black;">
<tr>
<td style="width:100px;">Blue</td>
<td style="width:100px;">White, Blue, and Green</td>
</tr>
<tr>
<td>Orange</td>
<td>Red, Orange, and Yellow</td>
</tr>
</table>

The table-layout:fixed; declaration in the table tag tells the browser that the table columns should not be adjusted after the first row.

The width:100px; in each td tag of the first row tells the browser how wide the columns should be.

The rest of the table, one more row in this case but however many rows the table might have, should conform to the column width specifications found in the first row.

It is indeed possible to have tables with column widths that you specify.

(This content first appeared in Possibilities newsletter.)


WillMasterBlog > CSS

Converting HEX and RGB Color Codes

HEX colors in HTML begin with a "#" character and are followed with either 3 or 6 hexadecimal digits. Hexadecimal digits range from 0 to F. Hexadecimal digits are case-insensitive. Example: #32Fe9A

RGB colors in HTML are specified with the rgb() or rgba() function. The function requires 3 numbers that range from 0 to 255, separated with commas. Example: rgb(50,254,154)

This article contains source code for a converter. You'll find it further below.

Here is a live implementation. Type a color in the left field and tap the "➜" button. Don't forget to bookmark if you think you may be using this page later.

The input field on the left may contain either a HEX color code or a RGB color code.

When you tap the button in the middle, the input field on the right will contain the converted code.

The converted color code may be copied from the right input field. When the right input field is tapped on, the content is selected and ready to copy.

HEX color code may be specified in the left input field with or without the leading "#" character. An example is #32FE9A

RGB color code is specified in the left input field with 3 numbers separated with commas. Optionally, the code may be specified as a full rgb() or rgba() color code. Examples are rgb(50,254,154) and rgba(50,254,154,.5) (rgba() for transparencies)

Conversions need to be done once in a while. As an example, you may prefer to use the HEX color code and have only the RGB available. This converter can do the conversion for you.

As another example, when you have a HEX color code and want to specify a degree of transparency, the HEX color needs to be converted so it can be used as a rgba() color. (Other conversions are possible, but the HEX color by itself can not be used for transparency.) A degree of transparency can be applied to text or background colors with a rgba() color specification.

Here is the source code for the converter. No modifications are required. Copy the code and paste it into your web page source code where you want the converter to publish.

<input 
   type="text" 
   id="conv_input" 
   style="width:10.6em; font-size:1rem; font-family:monospace;"> 
<input 
   onclick="ConvertNumber()" 
   type="button" 
   value="&#10140;"> 
<input 
   readonly="readonly" 
   onclick="select()" 
   type="text" 
   id="conv_response" 
   style="width:10.6em; font-size:1rem; font-family:monospace;">

<script style="text/javascript">
function dec2hex(d)
{
   d = parseInt(d);
   var h = d.toString(16);
   if( h.length < 2 ) { h = "0" + h; }
   return h.toUpperCase();
}
function hex2dec(h)
{
   if( h.length>1 && h.match(/^0/) ) { h = h.substr(1); }
   d = parseInt(h,16);
   return String(d);
}
function ConvertNumber()
{
   var response = document.getElementById("conv_response");
   n = document.getElementById("conv_input").value;
   n = n.replace(/rgba?/i,"");
   n = n.replace(/[^0-9a-f,]/ig,"");
   if( ! n.length ) { response.value=""; return; }
   if( n.match(/\,/) ) // rgb
   {
      var ta = n.split(/\,+/);
      if(ta.length!=3) { response.value="error"; return; }
      isok = true;
      for( var i=0; i<3; i++ )
      {
         if( ta[i]<0 || ta[i]>255 ) { isok = false; }
      }
      if( ! isok ) { response.value="error"; return; }
      var sta = new Array();
      for( var i=0; i<3; i++ ) { sta.push(dec2hex(ta[i])); }
      response.value = "#" + sta.join("");
      return;
   }
   var s = new String();
   if(n.length==3)
   {
      s = "";
      for( var i=0; i<3; i++ ) { s+=n.substr(i,1)+n.substr(i,1); }
      n = s;
   }
   s = "";
   if( n.length != 6 ) { response.value="error"; return; }
   var sta = new Array();
   for( var i=0; i<3; i++ )
   {
      s = n.substr((i*2),2);
      sta.push(hex2dec(s));
   }
   response.value = "rgb(" + sta.join(",") + ")";
}
</script>

The converter is JavaScript so it will work online or offline. Once loaded into a browser, no additional server resources are required.

The converter is speedy. And it is forgiving with your format. The "#" character is optional — just specify 3 or 6 hexadecimal characters. The rgb() or rgba() parts of color coding are also optional — you can get away with just specifying 3 numbers separated with commas.

(This content first appeared in Possibilities newsletter.)


How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List
software index page

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC