Jump to content

predefined like captcha...


starvator

Recommended Posts

Hi, i was wondering if there was a way to put

if what is typed into a text box is equal to "chicken" and if what is put in box #2 is = to "dog" and if a box is checked (a i agree to the term yadeyadeya) then the form will submit a username from a mysql (which is the $session->username ) table to a text file?  Every user that submits the two correct codes will have their username posted in the text file.  Also, as if it was a captcha code, i do not want people to be able to get the codes by looking at the page source.

 

I hope i was clear enough, thanks!

Link to comment
Share on other sites

So you want to make a form that has two questions. On submit the page checks the answers, and if correct (chicken, dog) then save their "username" to a text file?

 

Yes there is a way, if you don't know how to code at all i would suggest googling "PHP for beginners". Go through them, write the code and learn from that (you will probably learn enough to do this).

 

If you're just looking for some advice on how to implement this;

Start with a HTML form that submits to page2.php.

page2.php will check the POST variables to see if they are correct, if they are;

Open a text file using fopen, fwrite into it the username (from form? or $_session variable), then fclose() file.

 

If you want someone to code this for you, i personally think you would benefit more if you leanred how to do it yourself. Especially at this basic level as some of the most important things to know are the basic logic and syntax.

 

-cb-

Link to comment
Share on other sites

If you want someone to code this for you, i personally think you would benefit more if you leanred how to do it yourself. Especially at this basic level as some of the most important things to know are the basic logic and syntax.

But all i want is to put this on my website.  So you are saying that i should:

<form name="verification" action="verification1.php" method="post">
Code 1: <input type="text" name="code1" /><br>
Code 2: <input type="text" name="code2" /><br>
<input type="checkbox" name="agree" value="yes" /> I agree to the terms<br>
<input type="submit" value="Submit" />

Then on verification1.php i should:

<? 
$content = $session->login($_POST['user']; 
$file = "textfile.txt"; 
$Saved_File = fopen($file, 'w'); 
fwrite($file, $content); 
fclose($file); 
?>  

So, i probably made some mistakes... but how do i verify thatr the checkbox is clicked and that box 1 and box 2 are the right codes?

 

THANK YOU VERYVERYVERY MUCH!

Link to comment
Share on other sites

(Please dont use short open tags, stick to the standard <?php it'll save you time and trouble trust me).

 

So i guess the $session object is what your teacher has provided you with to access mysql. But he hasnt explained how to use it and neither have you so i cant help you there im afraid.

 

1. Remember, $_POST variables are variables submitted to your page via a form. so you would need to add a "username" field to your form. (Unless your teacher expects you to asctually use $_SESSIONs, cant help you unless you explain your session object and why your using it).

 

2. For file editing, go to php.net/fwrite

 

3. For checking Form variables:

if($_POST['code1'] == "chicken" && $_POST['code2'] == "dog"){ ... }

 

-cb-

Link to comment
Share on other sites

(Please dont use short open tags, stick to the standard <?php it'll save you time and trouble trust me).

 

So i guess the $session object is what your teacher has provided you with to access mysql. But he hasnt explained how to use it and neither have you so i cant help you there im afraid.

I don't have a teacher, i am trying to learn on my own.  I do not really understand what you said there  :confused: :confused:

Can you please clarify? Thanks!

 

the $_SESSION is from the login script i am using... I am trying to make a submission form that works with my login script.

 

I am using jpmaster77 login system http://www.evolt.org/PHP-Login-System-with-Admin-Features if that helps

Link to comment
Share on other sites

I see ok, I assumed you quoted that first post for another reason. Anyway;

 

FYI: Short open tags are "<?", I would recommend using the traditional open tags: "<?php" in all cases. Much more portable.

 

The code you are using has a session class which stores the username, and a database class which has methods to gather user information. Like this:

$user_information = $database->getUserInfo($session->username);

 

"username" is a Property of the $session Object Instance. It is not protected so you can retrieve this property directly.

"getUserInfo()" is a Method of the $database Object Instance. It takes 1 Argument of username and returns the Users Data as an array from the Database.

 

So we call $database->getUserInfo() with the argument: $session->username. This will return an array of data that's saved in the DB like:

Array(
      "username"      =>      'mrbob',
      "password"      =>      '39762d0e7b8a8e8cf05b234f3d97a',
      "userid"            =>      '1',
      "userlevel"        =>     '1',
      "email"              =>     'example@email.com',
      "timestamp"      =>     '10245633'
)

 

 

The Form

<form name="verification" action="verification1.php" method="post">
Code 1: <input type="text" name="code1" /><br>
Code 2: <input type="text" name="code2" /><br>
<input type="checkbox" name="agree" value="yes" /> I agree to the terms<br>
<input type="submit" value="Submit" />

 

verification1.php

<?php

// Include Session Object (Which includes Other Files and Initializes all objects)
include('session.php');

// Get the user information from Database
$userinfo = $database->getUserInfo($session->username);

// Store results If Correct:
if($_POST['code1'] != "chicken"){
      exit('Code 1 Mismatch<br />');
}
if($_POST['code2'] != "dog"){
      exit('Code 2 Mismatch<br />');
}
if(!isset($_POST['agree'])){
      exit('You did not agree to the Terms & Conditions'); // If you get this even if you click, change != "yes" to != "checked"
}

$filename = "textfile.txt";

$f = fopen($file, 'a+'); # Check: http://php.net/fopen

fwrite($f, "\n",$email);  # Add email to file (On a new line)

fclose($f); # Close file.

echo('Success!');

?>

 

 

-cb-

Link to comment
Share on other sites

I see ok, I assumed you quoted that first post for another reason. Anyway;

 

FYI: Short open tags are "<?", I would recommend using the traditional open tags: "<?php" in all cases. Much more portable.

 

The code you are using has a session class which stores the username, and a database class which has methods to gather user information. Like this:

$user_information = $database->getUserInfo($session->username);

 

"username" is a Property of the $session Object Instance. It is not protected so you can retrieve this property directly.

"getUserInfo()" is a Method of the $database Object Instance. It takes 1 Argument of username and returns the Users Data as an array from the Database.

 

So we call $database->getUserInfo() with the argument: $session->username. This will return an array of data that's saved in the DB like:

Array(
      "username"      =>      'mrbob',
      "password"      =>      '39762d0e7b8a8e8cf05b234f3d97a',
      "userid"            =>      '1',
      "userlevel"        =>     '1',
      "email"              =>     'example@email.com',
      "timestamp"      =>     '10245633'
)

 

 

The Form

<form name="verification" action="verification1.php" method="post">
Code 1: <input type="text" name="code1" /><br>
Code 2: <input type="text" name="code2" /><br>
<input type="checkbox" name="agree" value="yes" /> I agree to the terms<br>
<input type="submit" value="Submit" />

 

verification1.php

<?php

// Include Session Object (Which includes Other Files and Initializes all objects)
include('session.php');

// Get the user information from Database
$userinfo = $database->getUserInfo($session->username);

// Store results If Correct:
if($_POST['code1'] != "chicken"){
      exit('Code 1 Mismatch<br />');
}
if($_POST['code2'] != "dog"){
      exit('Code 2 Mismatch<br />');
}
if(!isset($_POST['agree'])){
      exit('You did not agree to the Terms & Conditions'); // If you get this even if you click, change != "yes" to != "checked"
}

$filename = "textfile.txt";

$f = fopen($file, 'a+'); # Check: http://php.net/fopen

fwrite($f, "\n",$email);  # Add email to file (On a new line)

fclose($f); # Close file.

echo('Success!');

?>

 

 

-cb-

 

Awesome! the verification works but now the submission doesnt work :(

 


PHP Error Message

Warning: fopen() [function.fopen]: Filename cannot be empty in /home/a3232513/public_html/verification1.php on line 22

Free Web Hosting

PHP Error Message

Warning: fclose(): supplied argument is not a valid stream resource in /home/a3232513/public_html/verification1.php on line 26

Free Web Hosting
Success!

 

What do these errors mean and what do I have to do to fix them? Do i need to change the file permissions?

 

THanks!!!!!!!!!!!!! :):):):):):)

Link to comment
Share on other sites

No it means I made a typo - look at the error and fix it, ill give you a hint: It says function.fopen - Filename cannot be empty, So what does it think is empty? $file. It is empty. But what should it be? :P

 

-cb-

 

I think i fixed it (  $f = fopen($filename, 'a+'); # Check: http://php.net/fopen ) but now i get the errors:

PHP Error Message

Warning: fopen(textfile.txt) [function.fopen]: failed to open stream: Permission denied in /home/a3232513/public_html/verification1.php on line 22

Free Web Hosting

PHP Error Message

Warning: fclose(): supplied argument is not a valid stream resource in /home/a3232513/public_html/verification1.php on line 24

Free Web Hosting
Success!

 

Permission denied means i need to chmod the file permissions but i want to keep this file secure so other people cant read it or view it... please help :)

Link to comment
Share on other sites

No it means I made a typo - look at the error and fix it, ill give you a hint: It says function.fopen - Filename cannot be empty, So what does it think is empty? $file. It is empty. But what should it be? :P

 

-cb-

 

ok i made it unsecure and stuff so it should work it sais success but it does not write anything to the text file... WHat is wrong?

Link to comment
Share on other sites

Great I'm glad you made it this far :).

 

Now all you need to do is define "$email", Use the $userinfo array, there should be an index with "email". If you want to check what is in the userinfo array you can try this on its own (then look at the html source it outputs in the browser):

 

test.php

<?php

// Include Session Object (Which includes Other Files and Initializes all objects)
include('session.php');

// Get the user information from Database
$userinfo = $database->getUserInfo($session->username);

// Show us what  is inside the userinfo array
print_r($userinfo);

?>

 

This should finish up the script. Also for security, i woudl place the text-file above the public root, eg: /home/a3232513/textfile.txt

 

This will prevent anyone from directly accessing it.

 

-cb-

Link to comment
Share on other sites

Also for security, i woudl place the text-file above the public root, eg: /home/a3232513/textfile.txt

 

This will prevent anyone from directly accessing it.

 

-cb-

 

how will i link to it then?  how will i tell the script that the location of the txtfile.txt is in /home/a3232513 and not public_html ?

Link to comment
Share on other sites

by specifying /home/a3232513/textfile.txt

rather than just textfile.txt (which means current directory).

 

You can also use "../textfile.txt" for specifying the directory above the current.

 

-cb-

 

OMG THANK YOU YOU ARE BEASTTTTTTTTTTTTTTTT

+1 for you!

 

Thanks again!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.