hi guys how r u doing ..
i need some help .. something went wrong and i dunno what is going on ..
i will post the problems .. i hope someone could help

first i tried making a script .. to include certain files depending on the time ...

<?php
if((H >= 06 && H < 08) || (H >= 18 && H < 20))include 'includes/iboerderij.php';
elseif((H >= 08 && H < 10) || (H >= 20 && H < 22))include 'includes/inatuurlijk.php';
elseif((H >= 10 && H < 12) || (H >= 22 && H < 00))include 'includes/ivijvers.php';
elseif((H >= 12 && H < 14) || (H >= 00 && H < 02))include 'includes/iformeel.php';
elseif((H >= 14 && H < 16) || (H >= 02 && H < 04))include 'includes/imodern.php';
elseif((H >= 16 && H < 18) || (H >= 04 && H < 06))include 'includes/ioosters.php';	
?>

it seemed to be working first .. but now it ONLY display inatuurlijk.php
any ideas why? i tried echoing the time .. and i can see the time is just like
my local time .. which means there is something wrong with my script.


second problem .. is that i tried running some validation for a form with php.
i didnt write the whole script. i got idea's here and there.
it was working in the morning and now it keeps echoing the e-mail
address that i entered is wrong. it keeps saying that. although in the morning
it only did if the e-mail address was missing a @ for example ..

<?PHP
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
echo "<div id=\"contactinf\">";
if ((!$_POST['Name'] || !$_POST['Message'] || !$_POST['Email']))
{					
echo 'u didnt fill everything in'; 
}
elseif (!preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/', $_POST['Email']))
{
echo "wrong e-mail address";
}
else 
{
echo 'something';
}
echo "<div id=\"contact-area\">&nbsp;";	
}else
{
?>
<div id="contactinf">
<p>
something
</p>
<div id="contact-area">	
<form method="post"><label for="Name" id="Name">Naam:</label>
<input type="text" name="Name" value="<?$_POST['Name']?>" />
<label for="Email" id="Email">E-mail:</label>
<input type="text" name="Email" value="<?$_POST['Email']?>"/>
<label for="Message" id="Message">Bericht:</label><br />
<textarea value="<?$_POST['Message']?>" name="Message" rows="20" cols="20"></textarea>
<input type="submit" name="submit" value="Verzenden" class="submit-button" />
</form>
<?
}
?>

i hope someone can help.
thanks in advance

Dani AI

Generated

Short summary and the actual causes

As discovered, the hour-switch bug came from using an undefined bareword (the lone H) instead of a proper value. In PHP an undefined constant is assumed to be its name (as a string) and emits a notice/warning, which makes numeric comparisons behave unexpectedly. (php.net)

Fixes and safer pattern for the time-based include

Prefer getting a numeric hour and comparing integers rather than relying on a bare constant. For example, cast the date result to an int ((int)date('G')) and test ranges with if/elseif or a switch (as suggested) — and always use braces (as recommended) for clarity. Also avoid numeric literals with leading zeros (e.g. 06, 08) because PHP treats them as octal literals which can be error-prone or invalid. (php.net)

Form validation and the email issue

Email validation is more robust and simpler with PHP’s filter API (FILTER_VALIDATE_EMAIL) instead of brittle regexes; always trim() inputs first and escape any echoed values with htmlspecialchars() before re-displaying them in the form to prevent XSS and broken markup. If the form used short PHP tags or malformed echo syntax, that can also cause odd behavior — use full <?php/<?= style for portability. (php.net)

Quick debugging checklist (development only)

Enable full error reporting while debugging (E_ALL / display errors) so notices about undefined constants, parse errors, or invalid octal literals are visible; turn this off in production. Also validate each POST field with isset()/trim() before using it, and prefer the built-in filters for common checks. (php.net)

Notes: defining a constant (what fixed the original page) works as a quick patch, but using a properly typed variable and the checks above leads to more predictable, portable code.

Recommended Answers

All 9 Replies

Looks like you will need to tidy the first part by including braces and also look at the structure of the if elseif else statement.

Your code should be

<?php
if((H >= 06 && H < 08) || (H >= 18 && H < 20))
{include 'includes/iboerderij.php';}
elseif((H >= 08 && H < 10) || (H >= 20 && H < 22))
{include 'includes/inatuurlijk.php';}
else((H >= 10 && H < 12) || (H >= 22 && H < 00))
{include 'includes/ivijvers.php';}
else((H >= 12 && H < 14) || (H >= 00 && H < 02))
{include 'includes/iformeel.php';}
else((H >= 14 && H < 16) || (H >= 02 && H < 04))
{include 'includes/imodern.php';}
else((H >= 16 && H < 18) || (H >= 04 && H < 06))
{include 'includes/ioosters.php';	}
?>

i tried this already before it didnt work ..
i tried it now again like u said .. it didnt work either

is H a defined variable?

May be you need to wait for some of the Gurus to have a look.

Sorry it didn't work

it says on the website of php H is time

H is not a predefined variable by php that i know of.

try adding this to the top of the document:

define('H',date("H"));

thanks a lot .. this solved my first problem ..

do u have any tips for my second problem please?

try the case statement, this would make things alot easier to code and to read.

Regards

i solved the second problem at school
i think i deleted something by mistake ..
thanks guys ..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.