Help - Search - Members - Calendar
Full Version: Turning Code Into Function
Weborum Webmaster Forum > Web Page Design > PHP
Joe
I'm pretty new to placing code into functions ... hence why I'm not very good with it.

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>";
}
?>


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']);
?>


... maybe ... ?

Thanks for any help.
Joe
I have the first two working;

PHP
<?php

// Working SESSION check function.

function CheckSession($Ses) {
if(!
$Ses) {
  return
false;
} else {
   return
true;
}
}

if(!
CheckSession($_SESSION['session_name'])) {
echo
'<p>SESSION error!</p>';
} else {
echo
'<p>SESSION set.</p>';
}

// Working IP check function.

function CheckIP($CurIP, $StorIP) {
if(!
$CurIP == $StorIP) {
   return
false;
} else {
   return
true;
}
}

if(!
CheckIP($_SERVER["REMOTE_ADDR"], $_COOKIE[Blah]['IP'])) {
echo
'<p>IP error!</p>';
} else {
echo
'<p>IP match.</p>';
}

?>


Not sure if they're the most efficient way of doing it but they work smile.gif The last function I'm having trouble with though sad.gif
xanderman
Using functions is a realy great way to cut down the file size of your script, i'll give you a few examples here.

CODE

<?php
$yourscriptvar = "whatever";
//to beable to access this varable in a function you will need to add global $yourscriptvar in your function
function YourFunction ($param1)
{
    global $yourscriptvar;
    if($param1 == $yourscriptvar)
    //u get it im sure.
}
?>


i also notice you use if's and else;s for returning true and false, this is unneed

CODE

function CheckIP($CurIP, $StorIP)
{
    if(!$CurIP == $StorIP)
    {
       return false;
    }
    return true;
}

you can get away with this because as soon as that return is read the function is killed
thus if your if condition isnt met it will skip the return false and go to the return true
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.