|
creating functions
creating functions
Currently viewing this thread: 1 (0 members and 1 guests)
 March 2nd, 2003 07:27 PM
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
$rand = md5(uniqid(rand(),1)); //create random number
$random = substr($rand, 0, 10); //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
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
 March 2nd, 2003 07:51 PM
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
 March 2nd, 2003 08:14 PM
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
function create_id()
{
$rand = md5(uniqid(rand(),1)); //create random number
$random = substr($rand, 0, 10); //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
include("config.php);
?>
how would i then call the function to produce the id?
___________________
//damell
 March 2nd, 2003 08:37 PM
Neverside Newbie
Status: Offline!
<?php
function create_id() {
$rand = md5(uniqid(rand(),1)); //create random number
$random = substr($rand, 0, 10); //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
$id = create_id();
echo $id;
?>
___________________
RAAAH
 March 2nd, 2003 08:41 PM
excellent that works fine, i'm beginning to understand now, thanks creative/oxy.
___________________
//damell
 March 2nd, 2003 09:55 PM

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
function create_id()
{
$rand = md5(uniqid(rand(),1));
$random = substr($rand, 0, 10);
$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($result, 0, 0);
}
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
 March 2nd, 2003 10:00 PM
Back in the day I was born, and now here I am.
Status: Offline!
heres an example where you would use parameters
<?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
 March 2nd, 2003 10:30 PM
Sucker for women...
Status: Offline!
I dont really know anything about functions but i think this is where the problem is
dont you need to explain what it has to do, i think it should be
<?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
}
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)
 March 2nd, 2003 10:35 PM
oh hehe, thx for pointing the second one out murph 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 
___________________
//damell
 March 3rd, 2003 07:09 AM
It's not your PHP version 
The following syntax is illegal in 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
$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.
|