TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-16-2008, 07:15 PM   #1 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Help Small php error - Thank you in advance!

there is no error outputted but i know the problem is in the preg_match part. Some assistance would be great!
Thank you!


Rules for preg:match, and to blur for me to read ;)
PHP Code:
int preg_match string $pattern string $subject [, array &$matches [, int $flags [, int $offset ]]] ) 
The script is small and might be ugly but it is only a practice run ;) so no flame :)

PHP Code:
<?php
error_reporting
(E_ALL E_NOTICE);

include 
"config.php";

if(isset(
$_POST['submit']))
{

$url=$_POST['url'];
if(
strlen($url)<1

  
{

      print 
"You did not enter a URL.";

  

  }

    
  }
if (
preg_match('~^http://[a-z]{2,3}\.youtube\.com/\?v=[\w-]+(?:&feature=related)?$~i','',$url)) {
 
    
$url="INSERT into upload (link) values('$url')";

    
mysql_query($url) or die(mysql_error());

    print 
"Link added!";

}

else

  {

    print 
"<form action='index.php' method='post'>";

    print 
"URL(include http://):<br>";

    print 
"<input type='text' name='url' size='20'><br>";

    print 
"<input type='submit' name='submit' value='submit'></form>";

 }





?>
Code;Freeek,.
codefreek is offline  
Reply With Quote
Old 10-16-2008, 07:46 PM   #2 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

PHP Code:
preg_match('~^http://[a-z]{2,3}\.youtube\.com/\?v=[\w-]+(?:&feature=related)?$~i','',$url
supposed to be:

PHP Code:
preg_match('~^http://[a-z]{2,3}\.youtube\.com/\?v=[\w-]+(?:&feature=related)?$~i'$url
I'm not sure if you have to use matches, since you just want 1 match; true or false.
__________________
Tanax is offline  
Reply With Quote
The Following User Says Thank You to Tanax For This Useful Post:
codefreek (10-17-2008)
Old 10-16-2008, 08:01 PM   #3 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

still the same..
codefreek is offline  
Reply With Quote
Old 10-16-2008, 08:41 PM   #4 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Help

CODE EDIT:

PHP Code:
<?php
error_reporting
(E_ALL E_NOTICE);

include 
"config.php";

if(isset(
$_POST['submit']))
{


$url=$_POST['url'];
if(
strlen($url)<1

  
{

      print 
"You did not enter a URL.";

  

  }

   else
   
   {
  
$check = (preg_match('~^http://[a-z]{2,3}\.youtube\.com/\?v=[\w-]+(?:&feature=related)?$~i','',$url));
if(
$check TRUE)
{
    
$url="INSERT into upload (link) values('$url')";

    
mysql_query($url) or die(mysql_error());

    print 
"Link added!";
}
}
}

else

  {

    print 
"<form action='index.php' method='post'>";

    print 
"URL(include http://):<br>";

    print 
"<input type='text' name='url' size='20'><br>";

    print 
"<input type='submit' name='submit' value='submit'></form>";

 }





?>
codefreek is offline  
Reply With Quote
Old 10-16-2008, 09:35 PM   #5 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

I tried your code and the preg_match is always returning a value, but I am guessing you just want to catch url's that come from youtube? it's a regex problem, I am not an expert in that area, but it would help to know what are you trying to match in the url.
tony is offline  
Reply With Quote
Old 10-16-2008, 09:49 PM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Your call to preg_match isn't going to work, you need to follow preg_match(pattern, test_string) for what you're trying to do with it. Also, in the if statement below it, you're assigning TRUE to the $check variable; not comparing its value to TRUE, use the double equals operator ($check == TRUE) (or ($check === 1), or simply ($check)).
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
codefreek (10-16-2008)
Old 10-16-2008, 10:12 PM   #7 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

If you don't mind me asking, what do you mean by test_string ?..

PS: Thank you for the help everyone!
codefreek is offline  
Reply With Quote
Old 10-16-2008, 10:19 PM   #8 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

The string you want to search in with the expression.
__________________
Tanax is offline  
Reply With Quote
The Following User Says Thank You to Tanax For This Useful Post:
codefreek (10-17-2008)
Old 10-16-2008, 10:52 PM   #9 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

Now when i try to submit anything i get blankpage :S?
codefreek is offline  
Reply With Quote
Old 10-16-2008, 11:11 PM   #10 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

because your preg_match is returning 0 or false I guess, either that or the script is failing further up, put an else on the preg_match if statement and see if it shows, this will tell you that there might be a problem with the regex (if the string passed is known to be a valid string)

Also its pointless assigning the result of preg_match to a variable in this case as it will only result in the number of times the pattern matched and because it stops at the first match, this will be either 0 or 1 (or false if it failed in some way). As PHP uses 0 and 1 as true and false the result will only ever be true or false.

This would be a better way of doing it:
PHP Code:
if(preg_match('pattern','string to test'))
{
    
// Do something
}
else
{
    
// Do something else

__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
codefreek (10-17-2008)
Old 10-17-2008, 12:37 AM   #11 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

@sketchMedia it works now, no blank page anymore but..
it wont block out none youtube links i can write bla bla and it will accept it..
any ideas?

Thank you..
codefreek is offline  
Reply With Quote
Old 10-17-2008, 01:26 AM   #12 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

You don't have an else for if the link is invalid. That would be the reason for your blank page. Try this code:

PHP Code:
error_reporting(E_ALL E_NOTICE);
include 
"config.php";

if(isset(
$_POST['submit']))
{
    
$url=$_POST['url'];
    
    if(
strlen($url)<1
    {
        print 
"You did not enter a URL.";
    }
    else
    {
        if (
preg_match('~^http://(?:[a-z]{2,3}\.)?youtube\.com/watch\?v=.+$~i'$url))
        {
            
$url="INSERT into upload (link) values('$url')";
            
mysql_query($url) or die(mysql_error());
            print 
"Link added!";
        }
        else
        {
            print 
"Link is invalid";    
        }
    }
}
else
{
    print 
"<form action='YouTube.php' method='post'>";
    print 
"URL(include http://):<br>";
    print 
"<input type='text' name='url' size='20'><br>";
    print 
"<input type='submit' name='submit' value='submit'></form>";

__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
codefreek (10-17-2008)
Old 10-17-2008, 01:59 AM   #13 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

Thank you, Wildhoney! you're the best!

PS: anyone know how i can block out so that you can not post the same link twice, maybe just do a check from db and see if that exists then print out this already in db ?
any ideas! thank you!
codefreek is offline  
Reply With Quote
Old 10-17-2008, 10:29 AM   #14 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Use 'IGNORE' on the INSERT statement.
sql Code:
INSERT IGNORE INTO `upload` (`link`) VALUES('$url')
Should stop duplicates.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
codefreek (10-17-2008)
Old 10-17-2008, 02:33 PM   #15 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

sketchMedia, it did not work.
codefreek is offline  
Reply With Quote
Old 10-17-2008, 03:00 PM   #16 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
Should stop duplicates.
Only if the link column has a unique index on it. The important thing is the index, it's up to the dev if they want to use ignore or go with the error generated when an insert tries to duplicate a unique key.
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
codefreek (10-17-2008)
Old 10-17-2008, 03:02 PM   #17 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

so if i change it to a unique.. it would work?
would it also make other problems?
codefreek is offline  
Reply With Quote
Old 10-17-2008, 03:04 PM   #18 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Help

PHP Code:
$results mysql_fetch_array($query);
foreach(
$results as $result) {
if(
$result == $url) {
die (
'SAMELINK!');
}

this is what tanax told me to try..


THANK YOU TANAX!
codefreek is offline  
Reply With Quote
Old 10-17-2008, 05:23 PM   #19 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Whilst that does work, just bear in mind that Salathe's approach is the more professional approach. You would make the link column an index, and set the unique property on it, you would then handle the response from MySQL.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
codefreek (10-17-2008)
Old 10-17-2008, 05:28 PM   #20 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

could anyone give an example on how to do that?
and thank you for taking time to help out
i am grateful!
codefreek is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:28 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design