Log in

View Full Version : Um... script? Please read!



Envy
04-13-2010, 10:53 PM
I'm in search for a script that allows me to do the following.

*Enter a user name
*Click submit to be redirected to a specified link
*Data from the form should be saved and displayed in a .txt file
*The form's data should log the users' ip address and date they submit the form

Can anyone help me?

BLiZZaRD
04-13-2010, 11:27 PM
Well, it depends on how you want it and what you are able to use. For instance PHP will do what you want, but will your server run PHP or is it an ASP server? Is the redirect on your site or to another site. Does the form only have a user name field or is this a complete form with tons of other stuff such as check boxes and radio buttons. Do you require a notification email, should that be sent to the user as well? When you say "display the .txt file" do you actually want it displayed on the page? So anyone filling out the form can see the info that everyone else has filled out?

Are you only saving user name and IP?

What is the purpose? Have you seen an example you can link to?

....

Envy
04-13-2010, 11:34 PM
Well, it depends on how you want it and what you are able to use. For instance PHP will do what you want, but will your server run PHP or is it an ASP server? Is the redirect on your site or to another site. Does the form only have a user name field or is this a complete form with tons of other stuff such as check boxes and radio buttons. Do you require a notification email, should that be sent to the user as well? When you say "display the .txt file" do you actually want it displayed on the page? So anyone filling out the form can see the info that everyone else has filled out?

Are you only saving user name and IP?

What is the purpose? Have you seen an example you can link to?

....

My web server will run PHP as far as I know, I've been using a couple of PHP scripts and they work fine. The redirect will be to another website, not my own. The form I need only needs a user name field as long as it's able to log at least the date the form was submitted and the user name that was inputted in the field. An email notification isn't needed and yes, anyone may view the .txt file because it shouldn't matter, but I'll try my best to keep it away from the public.

So really, I only need the user name and the date the form was submitted to save and be displayed to prevent cheating. The purpose of this is to log votes for my website. There are top list websites that allow people to vote, etc., and whatever website has the most votes gets to the top of the list, which produces more traffic through the number one ranked website. I'm trying to figure out how to create this because I'd like to offer special forum rewards and what not to my users to help out the community. I've seen this used on several other websites, but unfortunately I do not have a link to an example.

BLiZZaRD
04-14-2010, 12:28 AM
Well, what you are after is a little more complicated. But do able. Would be done by now if I wasn't sneaking peeks in from work.

This will get you started:



<?php
session_start();
$userName = $_POST["user"];
$agent = $_SERVER['HTTP_USER_AGENT'];
$uri = $_SERVER['REQUEST_URI'];
$ip = $_SERVER['REMOTE_ADDR'];
$ref = $_SERVER['HTTP_REFERER'];
$visitTime = date("r"); //Example: Thu, 21 Dec 2000 16:01:07 +0200
if($_SESSION["logged"] != "yes")
{


$logLine = "$visitTime - IP: $ip || User Agent: $agent || Page: $uri || Referrer: $ref\n";
$fp = fopen("visitorLog.txt", "a");
fputs($fp, $logLine);
fclose($fp);
$_SESSION["logged"] = "yes";
}
?>


Basically it will track the time, page they came from, and the IP into a file named visitorlog.txt.

Next I will work on displaying the user name and the form for that as well as sending them to another page upon submission. Sorry its not all in one piece, doing it as I can, in between boss walking by my desk and doing my paying job :)

Envy
04-14-2010, 12:32 AM
Well, what you are after is a little more complicated. But do able. Would be done by now if I wasn't sneaking peeks in from work.

This will get you started:



<?php
session_start();
$userName = $_POST["user"];
$agent = $_SERVER['HTTP_USER_AGENT'];
$uri = $_SERVER['REQUEST_URI'];
$ip = $_SERVER['REMOTE_ADDR'];
$ref = $_SERVER['HTTP_REFERER'];
$visitTime = date("r"); //Example: Thu, 21 Dec 2000 16:01:07 +0200
if($_SESSION["logged"] != "yes")
{


$logLine = "$visitTime - IP: $ip || User Agent: $agent || Page: $uri || Referrer: $ref\n";
$fp = fopen("visitorLog.txt", "a");
fputs($fp, $logLine);
fclose($fp);
$_SESSION["logged"] = "yes";
}
?>


Basically it will track the time, page they came from, and the IP into a file named visitorlog.txt.

Next I will work on displaying the user name and the form for that as well as sending them to another page upon submission. Sorry its not all in one piece, doing it as I can, in between boss walking by my desk and doing my paying job :)

Haha, no problem, I fully understand and I appreciate you helping me get started. I can't wait till the rest of the bits are pieces are complete. :)

BLiZZaRD
04-14-2010, 12:40 AM
Just for clarification, and correct me were I am wrong....

You have a page people will go to with a text field for them to enter a user name and a submit button. They enter the user name and click submit. They are taken to another website. Their IP, time and user name are logged in a .txt file on your server for you to see.

I assume they come back to your page at some point? This is where their user name and time they clicked the submit button will be displayed?

Envy
04-14-2010, 12:43 AM
Just for clarification, and correct me were I am wrong....

You have a page people will go to with a text field for them to enter a user name and a submit button. They enter the user name and click submit. They are taken to another website. Their IP, time and user name are logged in a .txt file on your server for you to see.

I assume they come back to your page at some point? This is where their user name and time they clicked the submit button will be displayed?

Correct all the way until you say they eventually visit the page where everything is logged at, the .txt file. Once they have been redirected to the specified URL and their user name, date and ip address if possible has been logged, it's over and I will thank you forever. ;)

BLiZZaRD
04-14-2010, 12:51 AM
Oh! Even better. Okay I thought you wanted the info displayed.



So really, I only need the user name and the date the form was submitted to save and be displayed to prevent cheating.


I will see what I can do here real quick...

Envy
04-14-2010, 12:53 AM
Oh! Even better. Okay I thought you wanted the info displayed.



I will see what I can do here real quick...

Well yes, displayed that way I can see whom submitted the form, their user name, the date they did it, etc., what we've been discussing for a bit now. :P

BLiZZaRD
04-14-2010, 01:15 AM
Yes I get that now, I thought you wanted it displayed on the page.

Now that you don't it was very easy and I am all done. Here you go:



<?
$userName = $_POST["User"];
$agent = $_SERVER['HTTP_USER_AGENT'];
$uri = $_SERVER['REQUEST_URI'];
$ip = $_SERVER['REMOTE_ADDR'];
$ref = $_SERVER['HTTP_REFERER'];
$visitTime = date("r");
if ($_GET['submit']=="true")
{
$logLine = "$visitTime - IP: $ip || UserName: $userName || User Agent: $agent || Page: $uri || Referrer: $ref\n";
$fp = fopen( "visitorlog.txt", "a" );
fwrite( $fp, $logLine );
fclose( $fp );
header("Location: http://theOtherSite.com"); //Change this to the site you want them redirected to
}
?>
<html>

<head>
<title>User Info</title>
</head>

<body>
<form name="user" method="post" action="form.php?submit=true"> //Change 'form.php' to whatever the name of the file where this code is placed is called.
<p>User Name:&nbsp;<input type="text" name="User" value="User"></p>
<p align="center"><input type="submit" name="submit" value="Submit"></p>
</form>
</body>

</html>


Please note the two edit places ( followed by //Change this line.... ) Make those changes and you are off and running.

Envy
04-14-2010, 01:27 AM
Yes I get that now, I thought you wanted it displayed on the page.

Now that you don't it was very easy and I am all done. Here you go:



<?
$userName = $_POST["User"];
$agent = $_SERVER['HTTP_USER_AGENT'];
$uri = $_SERVER['REQUEST_URI'];
$ip = $_SERVER['REMOTE_ADDR'];
$ref = $_SERVER['HTTP_REFERER'];
$visitTime = date("r");
if ($_GET['submit']=="true")
{
$logLine = "$visitTime - IP: $ip || UserName: $userName || User Agent: $agent || Page: $uri || Referrer: $ref\n";
$fp = fopen( "visitorlog.txt", "a" );
fwrite( $fp, $logLine );
fclose( $fp );
header("Location: http://theOtherSite.com"); //Change this to the site you want them redirected to
}
?>
<html>

<head>
<title>User Info</title>
</head>

<body>
<form name="user" method="post" action="form.php?submit=true"> //Change 'form.php' to whatever the name of the file where this code is placed is called.
<p>User Name:&nbsp;<input type="text" name="User" value="User"></p>
<p align="center"><input type="submit" name="submit" value="Submit"></p>
</form>
</body>

</html>


Please note the two edit places ( followed by //Change this line.... ) Make those changes and you are off and running.

Everything has worked so far, but when I try to access visitorlog.txt, it tells me that the page isn't found. I've made sure it has been uploaded into the correct directory twice now... Do you have any ideas of why I'd continue to get 404 errors?

Edit: Excuse my ignorance, I was typing in vistorlog instead of visitor. :)

Thank you so much!

BLiZZaRD
04-14-2010, 01:34 AM
You are very welcome.

Envy
04-14-2010, 01:44 AM
I have one more question, hopefully you haven't left this thread completely and will never come back... :)

I've tested the script out a couple of times and it works flawlessly, but all of the data is placed on one line instead of having line breaks after every form is submitted. Is there anyway to fix this? I think I can manage without the line breaks but it is an eye sore.

BLiZZaRD
04-14-2010, 01:48 AM
Yup.

Change this line:



$logLine = "$visitTime - IP: $ip || UserName: $userName || User Agent: $agent || Page: $uri || Referrer: $ref\n";


To this:



$logLine = "$visitTime - IP: $ip || UserName: $userName || User Agent: $agent || Page: $uri || Referrer: $ref \n\n";

djr33
04-14-2010, 01:49 AM
I haven't followed all of the details in this discussion, but I think this answers your question:

This line determines the format:
$logLine = "$visitTime - IP: $ip || UserName: $userName || User Agent: $agent || Page: $uri || Referrer: $ref\n";

Note that \n is the representation of a line break. It's the same as an actual return, but it's shorthand within the code. You can also use a real line break in the text (if that doesn't look too strange within the code).


The real question here is what to do about the format:
Yes, it's problematic having it all on one line, but what do you want to do instead?
The way that it is setup now, you have || separating the information, and new lines separating the different instances of data.

Perhaps here's a better way:
$logLine = "$visitTime - IP: $ip
UserName: $userName
User Agent: $agent
Page: $uri
Referrer: $ref
----------------";

Does that look nicer to you? You can format it in any way you like, though the one-line, one-item format is very common and can be useful later if, for example, you ever want to insert this into a database. (It's possible to use the new format I suggested as well, just not as standard.)



EDIT: Looks like I posted at the same time as BLiZZaRD. His answer is for having two lines between the entries (a blank line separating them), and mine is to have each item of each entry as a new line. Either one will work, so choose what you like best.

BLiZZaRD
04-14-2010, 01:54 AM
Good catch Daniel, I left it one line instead of separating everything for ease of use. And it seemed a tad silly to just have a user name input box and a submit button as the only content on the page. So I figured there would either be more already, or more coming later ;)

Envy
04-14-2010, 02:03 AM
Good catch Daniel, I left it one line instead of separating everything for ease of use. And it seemed a tad silly to just have a user name input box and a submit button as the only content on the page. So I figured there would either be more already, or more coming later ;)


I haven't followed all of the details in this discussion, but I think this answers your question:

This line determines the format:
$logLine = "$visitTime - IP: $ip || UserName: $userName || User Agent: $agent || Page: $uri || Referrer: $ref\n";

Note that \n is the representation of a line break. It's the same as an actual return, but it's shorthand within the code. You can also use a real line break in the text (if that doesn't look too strange within the code).


The real question here is what to do about the format:
Yes, it's problematic having it all on one line, but what do you want to do instead?
The way that it is setup now, you have || separating the information, and new lines separating the different instances of data.

Perhaps here's a better way:
$logLine = "$visitTime - IP: $ip
UserName: $userName
User Agent: $agent
Page: $uri
Referrer: $ref
----------------";

Does that look nicer to you? You can format it in any way you like, though the one-line, one-item format is very common and can be useful later if, for example, you ever want to insert this into a database. (It's possible to use the new format I suggested as well, just not as standard.)



EDIT: Looks like I posted at the same time as BLiZZaRD. His answer is for having two lines between the entries (a blank line separating them), and mine is to have each item of each entry as a new line. Either one will work, so choose what you like best.

I'll try that right now and reply with the results... :)

Yeah, I only needed the form as I'm just making the rest of the content simple HTML.

Edit: It works perfectly, thank you so much, to the both of you for being so helpful.