function that can access and change global variables


Apart from the global keyword, is there any way that a function can access and change global variables?

You can also access global variables anywhere in your scripts with a built-in associative array called $GLOBALS. To access a global variable called $test within a function, you could reference it as $GLOBALS[‘test’]

Difference between the associative array and a numerically indexed array


Is there any difference between the associative array and a numerically indexed array in PHP?

The division between an associative array and a numerically indexed array is not absolute in PHP. Nevertheless, you should treat them separately because each demands different strategies for access and manipulation.

Array keys aren’t quoted


The keys in an associative array are strings, but in its default error reporting state the engine won’t complain if array keys aren’t quoted.

Omitting quotation marks for array keys is poor practice, however. If you use unquoted strings as keys and your error reporting is set to a higher-than-standard level, the engine will complain every time such an element is met. Even worse, if an unquoted array key coincides with a constant, the value of the constant will be substituted for the key as typed.

If the key is stored in a variable, you do not need to use quotation marks:

$agekey = “age”;
print $character[$agekey];

discover the number of elements in an array


I can discover the number of elements in an array, so should I use a for statement to loop through an array?

You should be cautious of this technique. If you are not absolutely sure that the array you are reading is indexed by consecutively numbered keys, you might get unexpected results.

script that redirect pages


hello,

looking for php form script with redirect pages features.
also need to capture data into either txt file or mysql database.

when user input info, like name, email, address, etc..
and several options like link1, link2, link3..
and click submit.

script need to capture info like name, email, address into mysql database
and depends on options that user choose,
if link1, need to redirect to url1
if link2, need to redirect to url2
if link3, need to redirect to url3.

anybody know this kind a script?

try this script

Code:
<?php
$add=$_POST['add'];
$name=$_POST['name'];
$redirect=$_POST['redirect'];
//connect to db first - this is a sample query
mysql_query("insert into users (`id`,`name`) values ('','$name');");
if($redirect)
{
header('Location: '.$redirect.'');
}
?>
<form name="form3" action="2.php" method="post">
<table width="95%"  border="0">
<tr>
<td width="20%">Your Name </td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td><span>Redir</span> </td>
<td><select name=redirect>
<option value="http://www.msn.com" selected>Msn</option>
<option value="http://www.google.com">Google</option>
<option value="http://www.yahoo.com">Yahoo</option>
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="Submit" />
<input type="hidden" name="add" value="1">
</td>
</tr>
</table>
</form>

want to escape ‘ and “


I want to escape ‘ and “. I can’t figure out how to do this.
I guess the easiest way is to just add a to them, but how to do that?
BTW this is to stop some innocent SQL injection on my site.

You can use the function addslashes() to escape all of the ” and ‘ characters.

The proper way to prevent SQL injection is to use parameterised queries.

also try this

Code:
<?php
$value = stripslashes($value);
// to strip the slash.
$value = mysql_real_escape_string($value);
// to add slash.
?>

PHP code help


I use:
$today = date(“F – j – Y”);

however I want to add a certain number of days to it.
Like: $nextweek = 7

How can I add those 7 days to the current date? and still keep it in the same format.

this function u can use with MySQL database

Code:
function dt_only_date_minus_x_days( $x ) {
  return date("Y-m-d", time( ) - ( $x * 24 * 60 * 60 ) );
}

function dt_minus_x_days( $x ) {
  return date("Y-m-d G:i:s", time( ) - ( $x * 24 * 60 * 60 ) );
}

function dt_now( ) {
  return dt_minus_x_days( 0 );
}

function dt_yesterday( ) {
  return dt_minus_x_days( 1 );
}

Try his one
Code:
$time = time() + (7 * 24 * 60 * 60);
print $today = date("F - j - Y");              # today
print $today = date("F - j - Y", $time);    # next week

I think this will be useful for u
Code:
date("F - j - Y", strtotime('+1 week'));

interface the program with the Scanner


Hi all,

I want to include a feature in a program in php whereby we can import images automatically from a document placed in a “Scanner”.

How can I interface the program with the Scanner.

Any idea

So just to be absolutely clear, this is a ‘web app’ you want to do, right? If so, it can’t be done by PHP alone. In this instance, the PHP is a server side application that has no access to the client, basically.

I would guess for this scenario, you’re going to have to look at something like a Java applet or something.

Age verifcation code


Hello friends,

I am looking for a script or code to put it on a index page of adult site that asks for visitors age and verified it

can anyone help me with this please

why php, u can also do it with the use of HTML

why script, u can also do it with the use of HTML

No, I dont want this in HTML so that user has to go thru this every time.. i want it to set a cookie if user has already entered his/her age… once he dont have to do it again..

Use this PHP code

Code:
<?php

$verified = false;

if ($_POST['verify'])
{
    extract(array_map('intval', $_POST));

    if (!checkdate($month, $day, $year))
    {
        exit('Invalid birthday.');
    }

    $now = mktime(0, 0, 0);
    $birthday = mktime(0, 0, 0, $month, $day, $year);
    $age = intval(($now - $birthday) / (3600 * 24 * 365));

    if ($age >= 18)
    {
        setcookie('age_verified', 1, time()+3600*24*7, '/');
        $verified = true;
    }
    else
    {
        echo 'Sorry, you are too young.';
    }
}

?>

<?php

if (isset($_COOKIE['age_verified']) OR $verified)
{
    // Page content
}
else
{
?>
<form action="" method="post">
<select name="day">
<option>Day</option>

<?php

for ($day = 1; $day <= 31; $day++)
{
    echo "t". '<option value="'. $day .'">'. $day ."</option>n";
}
?>
</select>

<select name="month">
<option>Month</option>

<?php

for ($month = 1; $month <= 12; $month++)
{
    echo "t". '<option value="'. $month .'">'. $month ."</option>n";
}
?>
</select>

<select name="year">
<option>Year</option>
<?php

for ($year = date('Y')-80; $year <= date('Y'); $year++)
{
    echo "t". '<option value="'. $year .'">'. $year ."</option>n";
}
?>
</select>

<input type="submit" name="verify" value="Submit" />
</form>

<?php
}
?>