Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Storing ID in database
Old 05-07-2009, 05:29 AM Storing ID in database
Average Talker

Posts: 22
Name: john
Trades: 0
alright guys, i have a script here where it works perfectly fine. expect it stores the user ID in a txt. document can help me with storing it in my database. thanks!

PHP Code:
<?php
 
$userIP
$_SERVER["REMOTE_ADDR"];
$file"IPfile.txt";
 
if (
file_exists($file)) {
   
$fh fopen($file,"r")
   or die (
"Can't open file to read.");
   
   
#  Read file line by line and put into array
   #
   
while(!feof($fh))
   {
       
$fileContents[] = fgets($fh);
   }
   
fclose($fh);
 
   
#  Check lines for IP match
   #
   
foreach($fileContents as $line)
   {
            
       if(
$line $userIP){
             
$found "True";
             echo 
"IP already exists.";
             break;
       }
   }
}
## If IP not found will append userIP to end of file.  
## If file doesn't exist it will create file and write IP to first line.
 
if ($found != "True"){
   echo 
"<p>IP not found.<p>";
   
$fh fopen($file ,"a")
    or die (
"Can't open file to append.");
   
fwrite($fh"$userIP\n");
   
fclose($fh);
   echo 
"<p>File Updated.<p>";
}
 
?>
microsoftx is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-07-2009, 07:38 AM Re: Storing ID in database
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
I give you the logic, and let you try to code it.

1) gather the ip from the user
2) connect to the db
3) do a SELECT query with the gathered ip to chek if it exists
4) if it doesn't exists, make an INSERT query

And if your db have a UNIQUE INDEX on the field storing the ip, you can even avoid the check, as the db will refuse to insert a second time this ip.
A bit dirty, but it works.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 05-07-2009, 09:45 AM Re: Storing ID in database
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
It would be alot easier if you rewrote the db part of the script.
__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Storing ID in database
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.28769 seconds with 12 queries