mari
March 4, 2010, 10:58am
1
hi guys
what is the correct way to use a variable within a function from an external file
here is my external file (config.php )
<?php
$companyName = 'Testing';
?>
here is the file which i have the function in (validation.inc )
function validate_form($conn, $DEBUG) {
$validated = TRUE;
$validation_message = '';
global $companyName;
include 'config.php';
// check for spam
$scheck = isset($_REQUEST['scheck']) ? $_REQUEST['scheck'] : '';
if ($scheck) {
$validated = FALSE;
$validation_message = '<p>Spamer check failed.</p> ';
} else {
$spam_check = TRUE;
}
if ($spam_check) {
$required = array('title', 'first_name', 'last_name', 'email_address', 'phone', 'username', 'password', 'password_confirm');
foreach ($required as $field_name) {
if (!isset($_REQUEST[$field_name]) || !$_REQUEST[$field_name]) {
$formatted_field_name = str_replace('_', ' ', $field_name);
$validation_message .= '<li>You must provide '. $formatted_field_name .'</li> ';
$validated = FALSE;
}
}
$email_regex = '/^[a-zA-Z0-9\\._%+-]+@[a-zA-Z0-9\\.-]+\\.[a-zA-Z]{2,4}$/';
$company_regex = '/@$companyName\\.com$/';
// check the username is allowed (not already taken and valid for whatever)
if (isset($_REQUEST['username'])) {
if (!preg_match($company_regex, $_REQUEST['username']) ||
!preg_match($email_regex, $_REQUEST['username'])) {
$validation_message .= '<li>Username must be a valid $companyName email address.</li> ';
$validated = FALSE;
} else if (!check_user_name($conn, $_REQUEST['username'], $DEBUG)) {
$validation_message .= '<li>Username taken please try another.</li> ';
$validated = FALSE;
}
}
when i enter an invalid email address i recieve the error message
Username must be a valid $companyName email address.
it prints the variable name and not the string
Try encapsulating that string in double quotes (") instead of single quotes (').
Variable names in singel quotes are not parsed.
$validation_message .= '<li>Username must be a valid $companyName email address.</li> ';
should be :
$validation_message .= "<li>Username must be a valid $companyName email address.</li> ";
mari
March 4, 2010, 12:36pm
4
oops noticed something
can you not use the same varible twice in the function?
because it is being called twice and it appears the first time but not the second time?
* Username must be a valid Testing email address.
* You must have a $companyName email address.
i have used the double quotes on both lines
Can you post the updated code?
Then we can take a look
mari
March 4, 2010, 2:03pm
6
function validate_form($conn, $DEBUG) {
$validated = TRUE;
$validation_message = '';
global $companyName;
include 'config.php';
// check for spam
$scheck = isset($_REQUEST['scheck']) ? $_REQUEST['scheck'] : '';
if ($scheck) {
$validated = FALSE;
$validation_message = '<p>Spamer check failed.</p> ';
} else {
$spam_check = TRUE;
}
if ($spam_check) {
$required = array('title', 'first_name', 'last_name', 'job_title', 'email_address', 'phone', 'username', 'password', 'password_confirm');
foreach ($required as $field_name) {
if (!isset($_REQUEST[$field_name]) || !$_REQUEST[$field_name]) {
$formatted_field_name = str_replace('_', ' ', $field_name);
$validation_message .= '<li>You must provide '. $formatted_field_name .'</li> ';
$validated = FALSE;
}
}
$email_regex = '/^[a-zA-Z0-9\\._%+-]+@[a-zA-Z0-9\\.-]+\\.[a-zA-Z]{2,4}$/';
$company_regex = "/@$companyName\\.com$/";
// check the username is allowed
if (isset($_REQUEST['username'])) {
if (!preg_match($company_regex, $_REQUEST['username']) ||
!preg_match($email_regex, $_REQUEST['username'])) {
$validation_message .= "<li>Username must be a valid $companyName email address.</li>";
$validated = FALSE;
} else if (!check_user_name($conn, $_REQUEST['username'], $DEBUG)) {
$validation_message .= '<li>Username taken please try another.</li> ';
$validated = FALSE;
}
}
// validate the email address more
$email_address = isset($_REQUEST['email_address']) ? $_REQUEST['email_address'] : '';
//$email_regex = '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$/';
$site_regex = '/@site\\.com$/';
if ($email_address) {
if (!preg_match($email_regex, $_REQUEST['email_address'])) {
$validation_message .= '<li>You must provide a valid email address.</li> ';
$validated = FALSE;
} else if (!preg_match($company_regex, $_REQUEST['email_address']) &&
!preg_match($site_regex, $_REQUEST['email_address'])) {
$validation_message .= "<li>You must have a $companyName email address.</li>";
$validated = FALSE;
}
}
Hmm, I can’t really see why it’s not being displayed properly the second time.
But as long as you don’t change it, you can use it as often as you want within the function.
mari
March 5, 2010, 11:50am
8
its working now :S i didnt change anything i think it must have been a server update issue or something
thanks for your help
mari
March 5, 2010, 12:16pm
9
another question
can you use varibles in email addresses for example i have this bit of code to check if the email is valid it has to be at a certain domain, the domain name is a global variable within the function
$email_regex = '/^[a-zA-Z0-9\\._%+-]+@[a-zA-Z0-9\\.-]+\\.[a-zA-Z]{2,4}$/';
$company_regex = '/@$companyname\\.com$/';
it doesnt seem to pick up the string assigned to the variable??
system
March 5, 2010, 12:24pm
10
single quotes strikes again
mari
March 5, 2010, 12:36pm
11
i tried the double quotes that didnt work either
i just tried this i assigned a new variable
$companyEmail = ‘@testing ’;
and changed the code to
$company_regex = “/$companyEmail\.com$/”;
taking out the ‘@’ sign and using that within the variable instead
that seems to work…but is this method incorrect?
system
March 5, 2010, 1:52pm
12
i tried the double quotes that didnt work either
you always says that.
programming requires much more attention
Which is it?
$companyname
$companyName
$companyemail
$companyEmail
I see a bunch of different variables being used throughout your script with different spellings.
Put this at the top of your page, it’ll help you debug your script:
error_reporting(E_ALL);