Your PHP Solutions Resource 

DevTek.Org Code Snippets




Getting the ID of a record just inserted into a MySQL DB

To get the value of an auto-incrementing field in a MySQL DB after you insert a record, use the following right after you do the mysql_query():

Of course the $idNum can be any variable name you'd like.

Some examples of use would be where you are building a trouble ticket system and want to show the customer their call ticket number. Another example would be if you were updating two or more MySQL servers where the master server auto-increments the ID field and you use that ID to insert a record on the other servers which do not auto-increment their ID field.

Update records on multiple MySQL Servers

This snippet uses a function to add a record to three different MySQL servers. There are a few things that must be in place to use this:

  1. All the servers must have the same database name.
  2. All the servers must have the same table name.
  3. All the servers must have the same field names.
  4. All the servers must have the same user with rights to connect from the server this function is running from. This user must have rights on the table.
  5. This function is written to just add/insert records in the DBs. It can be written to do edits and deletes too.

Timing a Chunk of Code

//Set up beginning of timer
$stime microtime(true);

Code to be timed here.

//Set up end of timer
$etime microtime(true);

//Display results
echo "Your code completed in " floor($etime $stime) . " seconds!";

Find a challenge/response e-mail list subscriber

I own and operate numerous technical help discussion lists. We had a problem with a person who had subscribed an address using a challenge / response system, the one where if they haven't previously authorized your e-mail address, you receive a message from the challenge response system stating you must reply to the e-mail to verify you're a human.

While in theory this might seem like a good system to have, it wreaks havoc on a discussion list because when someone posts a message to the list, they get that challenge e-mail. The problem with most of these systems is that it doesn't tell you which e-mail address generated the challenge, so for list owners it makes it nearly impossible to track that address down to remove it.

I wrote the below script to send an individual probe e-mail to each subscriber address and placed the ID from the database in the subject so that when I received the challenge back, I could match the ID with the address in the database.

This script assumes you have a database with two fields. One called "id" and one called "email".

<title>List Message Probe</title>
<body bgcolor="silver">

<P align=center><FONT size=10>List Message Probe</FONT></p>

<P align=center><FONT size=5>
The mail probe is being sent.<br>
Do <b>not</b> close this window until you see the "Probe complete" message.



$conn=mysql_connect($dbserver,$dbuser,$dbpassword) or die(mysql_error());
$select=mysql_select_db($dbname$conn) or die(mysql_error());

$sqlview="SELECT * FROM " $table;

while (
$email_to $r["email"];
$subject $r["ID"] . " - Please do not respond to this probe";
$message "Please ignore this message... We're tracking down a problem.
    Do *not* reply to this message. It'll only confuse the issue.
    Thank you.
    List Moderator
"Probe Complete.";

Lazy Man's Virtual Host

Let's say you have multiple domain names registered and they're all pointing to the same IP on your web hosting services server, but you want each domain to more or less live in its own world. The below snippet placed at the top of your main page in the root folder (i.e. index.php, default.php or whatever it is) will do it for you.

// Get the requested host from the host header sent by the browser and convert it
// to uppercase.
$hostRequested strtoupper($_SERVER['HTTP_HOST']);

// Testing if "DOMAIN1" is part of the requested host. Must be in CAPS.
// If so, redirect the browser into SOMEFOLDER1 and load index.php from there.
if (strstr($hostRequested"DOMAIN1")) {
header('Location: SOMEFOLDER1/index.php'); 
// Testing if "DOMAIN2" is part of the requested host. Must be in CAPS.
// If so, redirect the browser into SOMEFOLDER2 and load index.php from there.
elseif (strstr($hostRequested"DOMAIN2")) {
header('Location: SOMEFOLDER2/index.php'); 

You need to replace DOMAINx and SOMEFOLDERx with the domain and subfolders that match your needs. Remember, DOMAINx needs to be in caps but SOMEFOLDERx does not.

This was a quick hack for a friend of mine who ran into this problem. If you really want to do this right, try out one of the free DNS management services. I recommend MyDomain as I've been using them for many years and have never had any problems with their service. These services will allow you to do what's called "stealth" or "masked" redirects to do what the above script does.

PHP Easter Egg

Put the following in the URL after the .php page



Note: You can prevent this by setting "expose_php = Off" in your php.ini

Copyright 2015
All rights reserved.
Powered by PHP   Powered by PSPad   Powered by MySQL   Last Modified
Aug 20, 2017 @ 18:15