Banner

Sponsor

Login


Welcome Back!
Guest
Guest

Register

Lost your password?

79 users online



creating functions

creating functions

Currently viewing this thread: 1 (0 members and 1 guests)


Page 1 out of 2
damell

damell

Status: Offline!

creating functions

I'm trying to make my code more efficient by creating seperate functions, such as login etc, so i can easily reuse them at a later date. So i thought i'd start off with my random session_id generator. Here is the code:

PHP:

<?php

$rand 
md5(uniqid(rand(),1)); //create random number
$random substr($rand010); //trim random number down to 10 characters
$hostname $_SERVER["REMOTE_ADDR"]; //get users ip address
$ip str_replace(".","",$hostname); //replace .'s in ip address
$id "$ip$random"//put strings together to make id
session_id($id); //set session_id 

?>

now how would i go about putting that in a function, what parameters do i put in for instance,

PHP:

<?php

function login($random$hostname)

?>

i'm really unsure as to how they work and any help would be great, my book doesnt go into too much depth about it, thanks.

___________________

//damell

shokka

shokka

Back in the day I was born, and now here I am.
Status: Offline!

you wouldnt need any parameters because you describe them all

I believe anyways....

___________________

shokka? - never coming | lazynezz > me

damell

damell

Status: Offline!

hmm, ok so if that's the case, how cud i then call the function in i included it on a page, so say config.php is:

PHP:

<?php

function create_id()
{
$rand md5(uniqid(rand(),1)); //create random number
$random substr($rand010); //trim random number down to 10 characters
$hostname $_SERVER["REMOTE_ADDR"]; //get users ip address
$ip str_replace(".","",$hostname); //replace .'s in ip address
$id "$ip$random"//put strings together to make id
session_id($id); //set session_id 
}

?>

then say index.php is

PHP:

<?php

include("config.php);

?>

how would i then call the function to produce the id?

___________________

//damell

Oxy

Oxy

Neverside Newbie
Status: Offline!
PHP:

<?php

function create_id() {
$rand md5(uniqid(rand(),1)); //create random number
$random substr($rand010); //trim random number down to 10 characters
$hostname $_SERVER["REMOTE_ADDR"]; //get users ip address
$ip str_replace(".","",$hostname); //replace .'s in ip address
$id "$ip$random"//put strings together to make id
session_id($id); //set session_id

return $id;
}

?>

Then in index.php, for example

PHP:

<?php

$id 
create_id();
echo 
$id;

?>

___________________

RAAAH

damell

damell

Status: Offline!

excellent that works fine, i'm beginning to understand now, thanks creative/oxy.

___________________

//damell

damell

damell

Status: Offline!

ok while we're on the subject of functions, i've added another one for login, however i keep getting T_STRING errors but on really strange lines, like i'll delete a line, the error will come up on the next line, so i'm guessing it's to do with the function in general, anyone see any problems with this?

PHP:

<?php

function create_id()

$rand md5(uniqid(rand(),1));
$random substr($rand010);
$hostname $REMOTE_ADDR;
$ip str_replace(".","",$hostname);
$id "$ip";

return 
$id
}

function 
login()
{
if(!isset(
$userNAME) && !isset($userPASS))
{
echo 
"
    <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\">
        <tr>
            <td valign=\"top\">
    <form method=\"POST\" action=\"$_SERVER['PHP_SELF']\">
    <table>
        <tr>
            <td>Username:</td>
            <td>
    <input type=\"text\" name=\"userNAME\">
            </td>
        </tr> 
           <tr>
            <td>Password:</td>
               <td>
    <input type=\"password\" name=\"userPASS\">
            </td>
        </tr>
           <tr>
            <td colspan=\"2\" align=\"center\">
    <input type=\"submit\" value=\"Login\">
    </form>"
;
}
else
{
$query "SELECT count(*) FROM dpost_users WHERE
         userNAME = '$userNAME' AND
         userPASS = password('$usesPASS')"
;
         
$result mysql_query($query) or die(mysql_error());
$count mysql_result($result00);    
}
if (
$count 0)
{
$valid_user $userNAME;
session_register("valid_user");

$query2 "UPDATE dpost_users SET userDATEONLINE = NOW(''), userTIMEONLINE = NOW('') WHERE userNAME = '$valid_user'";
$result2 mysql_query($query2) or die(mysql_error());

$query3 "SELECT * FROM dpost_users WHERE userNAME = '$valid_user'";
$result3 mysql_query($query3) or die(mysql_error());

$r mysql_fetch_array($result3);
$userSTATUS=$r["userSTATUS"];
}
}

?>

___________________

//damell

shokka

shokka

Back in the day I was born, and now here I am.
Status: Offline!

heres an example where you would use parameters

PHP:


<?php

function openandwrite($file$message) {  //create function that uses 2 parameters $file and $message
     
$open fopen($file"a"); //open the file
     
fwrite($open$message); // write to file
     
fclose($open);  //close the file
}

?>

$message would be a value given by a form and $file would be the file told to be opened.

So if in the form, I put in textfile.txt for the $file variable and write "hey whatsup" as the message, the function would open textfile.txt and write "hey whatsup" to it...

sorry, im bad at explaining things heh..

___________________

shokka? - never coming | lazynezz > me

murph

murph

Sucker for women...
Status: Offline!

I dont really know anything about functions but i think this is where the problem is

PHP:

<?php
login
()
?>

dont you need to explain what it has to do, i think it should be

PHP:

<?php
login
($username,$password)
?>

again im not sure its just a suggestion i hope someone who knows can clear this up..

also another error is here

PHP:

<?php
}
else
{
$query "SELECT count(*) FROM dpost_users WHERE
         userNAME = '$userNAME' AND
         userPASS = password('$usesPASS')"
;

?>


you spelled userPASS wrong, it should be userPASS not usesPASS

___________________

-Murph

Last edited by murph, March 2nd, 2003 10:32 PM (Edited 1 times)

damell

damell

Status: Offline!

oh hehe, thx for pointing the second one out murph Grin but i figured out the T_STRING error, was a problem with PHP_SELF, i just changed it from $_SERVER['PHP_SELF'] to plain old $php_self, something to do with my php version maybe Shocked

___________________

//damell

Jeb

Jeb

Status: Offline!

It's not your PHP version Smile

The following syntax is illegal in PHP:

PHP:

<?php

$var 
"This is the current page: $_SERVER['PHP_SELF']";

?>

You cannot use single quoted array indexes like that inside a string. You can remove the single quotes, but that is also bad practice.

Instead, use the curly bracket syntax, like so:

PHP:

<?php

$var 
"This is the current page: {$_SERVER['PHP_SELF']}";

?>

Works a charm.

___________________

Adam Goossens -- PHP is my mother tounge.

Linux: ( kernel.org | winehq ) -- f33l the p0w3r.

Nobody replying to your questions? Getting flamed? Getting told to RTFM? Ask your questions the right way.

Page 1 out of 2
Quick Jump:

Main Navigation


Site & Graphic Design by Aeon Tan
Developed by Jeremie Pelletier & Scott Roach


NeverAPI generated this page in 0.0135 seconds.