I have a snippet of code ... and to cut down on the amount of coding I want to try and place as much of it in a function as possible. The snippet is a login code, it continually checks (on each page) if the user is still logged in with 3 individual checks. These are;
- An IP check
- Encrypted username check
- Session check
All of these are set if the user is correctly logged in (via the login form). The IP and encrypted user name are both stored within cookies. The code I have at the moment is;
PHP
<?php
if(isset($_SESSION['session_name'])) {
echo '<p>correctly logged in.</p>';
} else {
echo '<p>session error ... bye bye birdie!</p>';
}
if($_SERVER["REMOTE_ADDR"] == $_COOKIE[Blah]['IP']) {
echo '<p>IP match! stay logged in</p>';
} else {
echo '<p>incorrect IP address ... bastardo!</p>';
}
$Enc_Username = $_COOKIE[Blah]['User'];
$sql = "SELECT Name FROM table_name WHERE Encrypted_Username = '$Enc_Username'";
$result = mysql_query($sql, $db);
$count = mysql_num_rows($result);
if($count == 0) {
echo '<p>do not pass go, do not collect $200.'."\n";
} else {
$row = mysql_fetch_assoc($result);
$Name = SafeAddSlashes($row['Name']);
echo "<p>your name : $Name</p>";
}
?>
if(isset($_SESSION['session_name'])) {
echo '<p>correctly logged in.</p>';
} else {
echo '<p>session error ... bye bye birdie!</p>';
}
if($_SERVER["REMOTE_ADDR"] == $_COOKIE[Blah]['IP']) {
echo '<p>IP match! stay logged in</p>';
} else {
echo '<p>incorrect IP address ... bastardo!</p>';
}
$Enc_Username = $_COOKIE[Blah]['User'];
$sql = "SELECT Name FROM table_name WHERE Encrypted_Username = '$Enc_Username'";
$result = mysql_query($sql, $db);
$count = mysql_num_rows($result);
if($count == 0) {
echo '<p>do not pass go, do not collect $200.'."\n";
} else {
$row = mysql_fetch_assoc($result);
$Name = SafeAddSlashes($row['Name']);
echo "<p>your name : $Name</p>";
}
?>
At the moment I don't have any redirection codes ... just messages ... I'm guessing though if this code were to be placed within a function I'd need return false if any of the checks were incorrect?
PHP
<?php
function CheckIP($CurIP, $StorIP) {
if(!$CurIP == $StorIP) {
return false;
} else {
return true;
}
CheckIP($_SERVER["REMOTE_ADDR"], $_COOKIE[Blah]['IP']);
?>
function CheckIP($CurIP, $StorIP) {
if(!$CurIP == $StorIP) {
return false;
} else {
return true;
}
CheckIP($_SERVER["REMOTE_ADDR"], $_COOKIE[Blah]['IP']);
?>
... maybe ... ?
Thanks for any help.