/    Sign up×
Community /Pin to ProfileBookmark

Match code in link with on in file and extract the line

Good day you all,
I work on a email confirmation system, I’m stuck on the part of matching the code in the confirmation link with the one in the file so I can find which line to move from the pending file to the confirmed file.

the lines in the file looks like that :

name, email, code
name, email, code

and the link look like :
[url]http://mysite.com/confirmed.php?code=123456[/url]

How can I look for the line in the file that match the code in the link

Thanks!

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 23.2009 — I'd strongly recommend doing this with a database instead of trying to manage two files like that. The reason is that the database will likely be quicker as the amount of data increases, plus you will not have to worry about and figure out how to handle potential race conditions, where two nearly simultaneous requests both want to read and write those files, with whichever one writes second overwriting the results of the first. This can be worked around by some sort of lock file mechanism, but this adds more complexity and processing. With a database solution, all you need is one table with an additional field to indicate the pending/confirmed status: when the link request is received all you have to do is update the row with the matching ID to change that status field.

If you insist on using a files-based approach, you'll probably want to use file() to read the first file into an array, loop through it with foreach() and explode() to find the element with the ID in question, append that element to the 2nd file via fopen()/fwrite(), unset() that element from the array, then write that array back over the first file using implode() and file_put_contents(). That already seems like a lot of work without even considering the file locking issue on the first file, when with a database solution you would just need to do something like:
[code=php]
if(!empty($_GET['code']))
{
$connx = mysql_connect('blah', 'blah', 'blah');
mysql_select_db('blah');
$sql = "UPDATE foobar SET confirmed=1 WHERE code=" (int) $_GET['code'];
mysql_query($sql);
}
[/code]

(You would, of course, want some additional error-checking code in that, but that is the gist of all that would be needed to change the status.)
×

Success!

Help @Peuplarchie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...