Link submit

This is a discussion on "Link submit" within the PHP Forum section. This forum, and the thread "Link submit are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jul 12th, 2007, 22:26
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Question Link submit

Hello.
I would like a page that people can go onto type in a name of a site and the URL, then it appear in a table on the page.
Preferably without MySQL because I don't understand it well..
but if it has to that's okay.

Also if possible being able to apply a validation rule so that people can't spam the page with lemonparty.org and goatse.cs

thanks!
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote

  #2 (permalink)  
Old Jul 12th, 2007, 23:50
Up'n'Coming Member
Join Date: Sep 2006
Location: UK
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Link submit

When links are submitted, append the URL and the title to a .txt file on a new line with some sort of separator, e.g. http://www.url.com|Title

To display the links:
Count the number of lines in the file.
e.g.
PHP: Select all

$file urls.txt;

// file of URLs

$lines file ($file);
// $lines is an array of lines in the file.

$num_lines count ($lines);
// number of lines in file 

Now, for each line, explode() it into the different parts and echo() it to the page.

e.g.
PHP: Select all

for ($i=0$i<$num_lines$i++){

// sets $i as zero; while $i is less than the number of lines in the file, this loop runs; add one to $i

$links explode("|"$lines[$i]);
// splits the links into the bit before and after the separator 

$url htmlspecialchars($links[0]);
$title htmlspecialchars($links[1]);
// prevent some XSS

echo("<a href=\"$url\">$title</a><br>");
// echo the link in it's constituent parts.


This works, but it would probably be much easier just to use a database.

-----------------------------------------------------------------------

EDIT: Forgot you asked about validation.
When the user submits it, perform a quick check before adding the data to a file:

e.g.
PHP: Select all

$blacklist = array("lemonparty""goatse""etc");

foreach (
$blacklist as $sites){
if (
preg_match("/$sites/i"$url)){
die(
"cannot submit this url");
}
if (
preg_match("/$sites/i"$title)){
die(
"cannot submit this title");
}

EDIT 2:
I have posted an alternative (better) way to display the links a bit further down in the topic.

EDIT 3
Commented the code to make it more readable.

Last edited by balaclave; Jul 13th, 2007 at 15:02.
Reply With Quote
  #3 (permalink)  
Old Jul 12th, 2007, 23:54
SuperMember

SuperMember
Join Date: May 2006
Location: North West, UK
Age: 22
Posts: 1,173
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Link submit

What's the difference between echo and print. Are they interchangeable?

Pete.
Reply With Quote
  #4 (permalink)  
Old Jul 13th, 2007, 00:07
Up'n'Coming Member
Join Date: Sep 2006
Location: UK
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Link submit

Quote:
Originally Posted by pa007 View Post
What's the difference between echo and print. Are they interchangeable?

Pete.
I have never thought about this before.
This seems to be one of the most referred to documents on the issue.
Reply With Quote
  #5 (permalink)  
Old Jul 13th, 2007, 00:12
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Link submit

utterly confused!
But I will read over it and test it.
Thanks!
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #6 (permalink)  
Old Jul 13th, 2007, 00:15
SuperMember

SuperMember
Join Date: May 2006
Location: North West, UK
Age: 22
Posts: 1,173
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Link submit

Quote:
Originally Posted by balaclave View Post
I have never thought about this before.
This seems to be one of the most referred to documents on the issue.
Pretty interesting, I suppose I won't go far wrong using print then. I haven't really used echo much but another book I have been reading through seems to use it all the time instead of print.

Thanks,

Pete.
Reply With Quote
  #7 (permalink)  
Old Jul 13th, 2007, 12:20
Up'n'Coming Member
Join Date: Sep 2006
Location: UK
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Link submit

I always use echo().

I just realised there is a problem with my code.
Using a colon as a seperator wouldn't work, because URLs have colons. :P
I changed it to a verticle line thing. ( | )



EDIT:
Forget that first post.
This works a lot better.

PHP: Select all

<?php

$file 
"urls.txt";
// filename of link information

$lines file ($file);
// puts each line of files in an array

foreach ($lines as $lines2 => $line) {
// loop that runs for each member of the array

    
$links explode("|"$line);
        
// splits the links into the bit before and after the separator 

    
$url htmlspecialchars($links[0]);    
    
$title htmlspecialchars($links[1]);
    
// prevent some XSS
    
    
echo("<a href=\"$url\">$title</a><br>");
    
// echo links

}

?>
EDIT 2
Commented the code.
The code in my first post in the topic is now working.

Last edited by balaclave; Jul 13th, 2007 at 15:02.
Reply With Quote
  #8 (permalink)  
Old Jul 13th, 2007, 15:45
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Link submit

Thanks for that
Gunna test it now.
and yeah I think that's about the only use for a pipe
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #9 (permalink)  
Old Jul 13th, 2007, 16:10
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Link submit

Okay it works perfectly
Just need to know how to set up a form so that people can submit a site to the file.
How do i do this?
Thanks again!
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #10 (permalink)  
Old Jul 13th, 2007, 16:15
SuperMember

SuperMember
Join Date: May 2006
Location: North West, UK
Age: 22
Posts: 1,173
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Link submit

Vertical line things "|" are your friend in JavaScript. The logical operator for 'or' is two vertical line things "||". I hate all of these stupid symbols those cos they are in ridiculous places on your keyboard. Can you get a key pad with just coding symbols on it? If you can't I'm going to make one. I don't know how but I will.

Pete.
Reply With Quote
  #11 (permalink)  
Old Jul 13th, 2007, 16:18
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Link submit

Actually I swear I saw one a while ago.
it was USB and about the size of the numpad on normal keyboards.
Not sure where I saw it though sorry!
My keyboards the worst, UK laptop keyboard, set up in US layout.
So when I want @ i have to do ".
annoying -_-
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #12 (permalink)  
Old Jul 13th, 2007, 16:54
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Link submit

Found a tutorial
should be okay for now
thanks!
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #13 (permalink)  
Old Jul 13th, 2007, 16:59
Up'n'Coming Member
Join Date: Sep 2006
Location: UK
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Link submit

Quote:
The logical operator for 'or' is two vertical line things "||"
That's the same in PHP.

Piping is one of the most powerful features of the *nix command console. It uses the pipe symbols.
See here.

Quote:
My keyboards the worst, UK laptop keyboard, set up in US layout.
You can fix that.
If you're using XP:
Start >> Control Panel >> Regional and Language Options >> Languages >> Details >> Select English (UK) >> Apply

Quote:
Just need to know how to set up a form so that people can submit a site to the file.
How do i do this?
HTML: Select all
 <FORM action="add.php" method="post">
    <P>
    Title: <INPUT type="text" name="title"><BR>
    URL: <INPUT type="text" name="url"><BR>
    <INPUT type="submit" value="Add link">
    </P>
 </FORM>
add.php:
PHP: Select all

<?php
$title 
$_POST['title'];
$url $_POST['url'];
// Get URL info

$title htmlentities($title);
$url htmlentities($url);
// Probably better to do the filter checking before adding, because IE parses text files and executes javascript in them....stupid IE.

$file "urls.txt";

$blacklist = array("lemonparty""goatse""etc");
foreach (
$blacklist as $sites){
   if (
preg_match("/$sites/i"$url)){
      die(
"cannot submit this url");
   }
   if (
preg_match("/$sites/i"$title)){
      die(
"cannot submit this title");
   }
}
//check against disallowed sites  

$urls fopen($file'a') or die("Error!!");
fwrite($urls"$url");
fwrite($urls"|");
fwrite($urls"$title");
fclose($urls);
// write to file

echo("Success! File added");

?>
This is untested at the moment.
I'm going out, I'll test it and fix anything that doesn't work.
(I think it will work.)
Reply With Quote
  #14 (permalink)  
Old Jul 13th, 2007, 17:04
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Link submit

you = lifesaver
fo sure
someone sticky this?
THANKS!
--------------
Just one thing
I need it to add to the text file on a different line.
at the moment it adds it to the same line e.g.
http://url.com/ |urlhttp://example.com/ |example.

when it needs to be,

http://url.com/ |url
http://example.com/ |example

I'm crap at php... so wouldn't know where to start )

---------------------------------

Okay all fixed now
Thanks!
Code: Select all
$urls = fopen($file, 'a') or die("Error!!");
fwrite($urls, "\n");
fwrite($urls, "$url");
fwrite($urls, "|");
fwrite($urls, "$title");
fclose($urls);
// write to file
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)

Last edited by alexgeek; Jul 13th, 2007 at 17:39. Reason: needed editing..
Reply With Quote
  #15 (permalink)  
Old Jul 13th, 2007, 17:47
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Link submit

Thanks balacave
Right everythings up and working
if you want to see,
go here http://www.alexgeek.co.uk/PHP/links.php
add your site if you like.
And I know I've asked for a lot...
but does anyone know how to set a time limit between submitting.
like the user submits an URL and needs to wait 30 seconds for another one.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #16 (permalink)  
Old Jul 13th, 2007, 23:49
Up'n'Coming Member
Join Date: Sep 2006
Location: UK
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Link submit

Quote:
I need it to add to the text file on a different line.
Whooops!
Didn't have time to bug check before going out.

This should work for your time-check thing.
See you and your flat files. Grrrrrrr.
PHP: Select all

<?php

$time 
time();
// unix timestamp (seconds since Jan 1st 1970)
$ip $_SERVER['REMOTE_ADDR'];
// ip of user
$file "ips.txt";
// file to write ips and times to.


$lines file ($file); // split file into array of lines

foreach($lines as $line => $match) { 
//for each line of file, do the following:

    
if (!preg_match("/$ip/"$match)) {
    
//if IP doesn't exist in file, echo the submit form
    
echo("
    <FORM action=\"add.php\" method=\"post\"> <P>
    Title: <INPUT type=\"text\" name=\"title\"><BR>
    URL: <INPUT type=\"text\" name=\"url\"><BR> <INPUT type=\"submit\" value=\"Add link\"> </P> </FORM>
    "
);
   } 
   else if (
preg_match("/$ip/"$match)){
    
// if ip is in file, split it into it's two parts
    
$ipstring explode("|"$match);
    
$checktime $ipstring[1];
    break;
    
// exit foreach loop (in case there is more than one ip in file)
    
   
}
}


if (
$time $checktime){
// if not yet 30 seconds
$timetowait $checktime $time;
echo(
"Please wait $timetowait seconds before submitting another link");
}

if (
$time >= $checktime) {
// if time is more than 30 seconds
echo("
    <FORM action=\"add.php\" method=\"post\"> <P>
    Title: <INPUT type=\"text\" name=\"title\"><BR>
    URL: <INPUT type=\"text\" name=\"url\"><BR> <INPUT type=\"submit\" value=\"Add link\"></p></FORM>
"
);


$newtime $time 30;
// time() + 30 seconds

$contents file_get_contents($file);
// get contents of file
$newcontents "$ip|$newtime\n" $contents;
// generate new content
// we need to do this because our preg_match loop will stop at the first instance, so we add the new 

details to the top of the file

$ips 
fopen($file'w') or die("Error!!");
fwrite($ips"$newcontents");
fclose($ips);
// create new file with ip and time


}

else {
(
"Ooops! Something went wrong");
// nothing matched anything

}

?>
It worked when I tested it.
Reply With Quote
  #17 (permalink)  
Old Jul 14th, 2007, 00:37
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Link submit

thanks again
probably be back in like 5 mins ha
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #18 (permalink)  
Old Jul 15th, 2007, 13:24
Up'n'Coming Member
Join Date: Sep 2006
Location: UK
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Link submit

I looked at your link submit page, and it would probably be better if you integrated the time delay checking with add.php, because at the moment, if you refresh the page before 30 seconds, the input box is removed.
i.e.

add.php
PHP: Select all


<?php

$time 
time();
// unix timestamp (seconds since Jan 1st 1970)
$ip $_SERVER['REMOTE_ADDR'];
// ip of user
$file "ips.txt";
// file to write ips and times to.


$lines file ($file); // split file into array of lines

foreach($lines as $line => $match) { 
//for each line of file, do the following:

    
if (!preg_match("/$ip/"$match)) {
    
//if IP doesn't exist in file, echo the submit form
    

      
$title $_POST['title'];
      
$url $_POST['url'];
      
// Get URL info

      
$title htmlentities($title);
      
$url htmlentities($url);
      
// Probably better to do the filter checking before adding, because IE parses text files and executes javascript in them....stupid IE.

      
$file "urls.txt";

      
$blacklist = array("lemonparty""goatse""etc");
      foreach (
$blacklist as $sites){
         if (
preg_match("/$sites/i"$url)){
            die(
"cannot submit this url");
         }
         if (
preg_match("/$sites/i"$title)){
            die(
"cannot submit this title");
        }
      }
      
//check against disallowed sites  

      
$urls fopen($file'a') or die("Error!!");
      
fwrite($urls"$url");
      
fwrite($urls"|"