Jump to content
Larry Ullman's Book Forums

Recommended Posts

I'm sorry - I understand what you are saying - and if I saw the code, I would understand it... I just can't write it! I have no idea where to even start! Can you give me any other clues? It's not that you're not great at explaining things... It's just that I don't know how to put what I want it to do in the syntax that is understood by the server!

Link to post
Share on other sites
  • Replies 64
  • Created
  • Last Reply

Top Posters In This Topic

Close,

 

$data = file_get_contents($file);

 

This way you assign the returned value of the contents of the file, which you stored the location at $file. Now you have the contents of that file in the variable $data. So now you need to somehow check the form input against the contents in $data.

Link to post
Share on other sites

I did this and my file still doesn't work, but I suspect that I now need to work on the if statement line? I know that if ($_POST['username'] is correct. I am not sure about the rest -

$data = file_get_contents($file); //Reads the file contents

 

if ($_POST['username'] != '$data') {

$problem = TRUE;

print '<p class="error">That username has been taken</p>';

}

Link to post
Share on other sites

This is what what echoed back to me... Looks like the usernames and the encrypted passwords times 2.

 

Register

 

sandy 2a2d595e6ed9a0b24f027f2b63b134d6 13214921493233 sandy 2a2d595e6ed9a0b24f027f2b63b134d6 13214925052444 April 2a2d595e6ed9a0b24f027f2b63b134d6 13214925133957 Brent 2a2d595e6ed9a0b24f027f2b63b134d6 13214925213887 Lexi 2a2d595e6ed9a0b24f027f2b63b134d6 1321492609494 Noah 2a2d595e6ed9a0b24f027f2b63b134d6 1321492640686 Aubree 2a2d595e6ed9a0b24f027f2b63b134d6 13214927182237 Ally 2a2d595e6ed9a0b24f027f2b63b134d6 1321492764580 austin 2a2d595e6ed9a0b24f027f2b63b134d6 13214959482343 Dad 657f8b8da628ef83cf69101b6817150a 13214973592546 mom 716f6b30598ba30945d84485e61c1027 13214974674171 Jim 098f6bcd4621d373cade4e832627b4f6 1321502406245 Jason 098f6bcd4621d373cade4e832627b4f6 13215024281741 sister 5f4dcc3b5aa765d61d8327deb882cf99 13215025103858 sister 098f6bcd4621d373cade4e832627b4f6 13215025541571

You are now registered!

Link to post
Share on other sites

Ok, so now you can see whyyour if statement will fail. Because all that information will be returned in one big string. So you need to now look through php.net or google for some kind of function where you partially match a string. This way you can use the $_post['username'] value to see if it appears somewhere in the $data string.

Link to post
Share on other sites

Hi there.... After I thought about quitting my class - I wasn't happy with myself! I decided to go back to square one - and read the book - and I got to Chapter 6 - where it talks about the control structures and I did find out where my hang up was.... Then I also came across this function - and I just know I'm onto something... But when I run this script - the only thing that is printed is Register.... which is my html h1 tags. So none of my php is coming through.... Dreamweaver isn't showing me that I have any syntax errors....

 

$data = file_get_contents($file); //Reads the file contents

$data = strtok($data);

 

if ($_POST['username'] == $data) {

 

print '<p>That username has been taken, please try again</p>';

Link to post
Share on other sites

I even tried this line of code instead....

$data = file_get_contents($file); //Reads the file contents

$data = strtok($data, " ");

 

while ($_POST['username'] == $data) {

print '<p>That username has been taken, please try again</p>';

}

 

But at any rate, I think I didn't have an statement closed - and the code is functional now - but it is not producing the error message when I enter a duplicate name....

 

if (is_writable($file)) { // Open the file.

 

$data = file_get_contents($file); //Reads the file contents

$data = strtok($data, " ");//separates the contents of the $data into individual words

 

if ($_POST['username'] == $data) { // compares the indivdual words from the $data file to the what the user entered in the username box

$problem = TRUE;

print '<p>That username has been taken, please try again</p>';

}

Link to post
Share on other sites

If ($_POST['username'] == $data) { // compares the indivdual words from the $data file to the what the user entered in the username box

 

Does your comment here actually happen though? What does echo $data return just before your if statement?then you may see why its failing.

Link to post
Share on other sites

Here is what it returned...

April 2a2d595e6ed9a0b24f027f2b63b134d6 1321586320291 Aaron 098f6bcd4621d373cade4e832627b4f6 13215878883365 Tammy daffd55e1b8020c7a60a7b6e36afb775 13215880821420 Tammy daffd55e1b8020c7a60a7b6e36afb775 13215882131973

 

That is the contents of my text file. What exactly am I looking for? What should it echo?

Link to post
Share on other sites

So to use strtok you need to break up the string. So uses php.net to check how this function works to do that. Also look at how you are updating the txt file, what token is being used when you write the data. There is an easier way to do this, but as I said I don't have the book as you said this function is used we'll continue with it.

Link to post
Share on other sites

well - the other function I was toying with was this... Although that isn't producing the outcome the author wanted either. Is this the other way you were referring to?

while($line = fgetcsv($newdata, 10000, " ") ) {

 

if ($_POST['username'] == $newdata) { // compares the indivdual words from the $data file to the what the user entered in the username box

$problem = TRUE;

break;

}

fclose($fp);

print '<p>That username has been taken, please try again</p>';

Link to post
Share on other sites

Lets stick with strtok() as it's in the book. Have you looked at the PHP manual for this function. You need to split the string into smaller pieces and then check the string pieces against the username. That's essentially the logic. The manual gives you over half the answer to your problem. I can't stress how useful and important it is to learning php. Its often neglected by people new to PHP, but its really good. Make it your best friend ;)

Link to post
Share on other sites

I have looked at the manual - and but the context used "isn't in English" to me yet. I have a tough time translating their context.... But I'll keep plugging away - at least I know I'm on the right track.

 

The strtok() function was explained about 4 chapters back - but when I was re-reading the book - that's when I came across it again and thought it might be a good fit for this purpose.

 

However, the getcsv() function was described in this chapter and after I read about it in the manual - I thought that one might work too. I needed to find a function and stick with it....but I didn't know which to choose! I will take your recommendation and stick with the strtok() function and work from there....

 

I needed a little affirmation that I was using the correct function - thanks. I'll keep pluging away and get back to you...

Link to post
Share on other sites

I re-read the user manual - and I am going to have you look at my comments - Apparently, my data isn't separated. What function am I missing to call?

 

$newdata = file_get_contents($file); //Reads the file contents

$data = file_get_contents($file); //Reads the file contents

$data = strtok($data, " ");//separates the contents of the $data into tokens - the content that needs separating is the $data and the token is the space which is found in " ". Now - $data should be separated data

 

if ($_POST['username'] == $data) { // compares the individual words from the $data file because they are now separated into tokens to the what the user entered in the username box

$problem = TRUE;

print '<p>That username has been taken, please try again</p>';

}

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...