Can anybody please help me? I pay $20

Discussion in 'PHP' started by misterdh, Jul 7, 2010.

  1. #1
    Hi

    I am a newbie in PHP and have been trying to solve this problem for days without any luck.. I tested alot by myself, asked in plenty of forums and even asked my previous programmer for help without any luck..

    So now I want your help and I will pay $30 via PayPal to the person who figure this out.

    Here is the problem:

    I am trying to redirect a page to another page when a certain keyword is submitted in a text field. Both the keywords and the URL´s it will redirect to will be pulled from a database.

    My code looks like this:

    <?php
    
    ini_set("display_errors", true);
    error_reporting(-1);
    
    header("Location: $redir");
    exit;
    
    include("connect.php");
    
    $extract = mysql_query("SELECT * FROM engines");
    $numrows = mysql_num_rows($extract);
    $i = "0";
    while ($row = mysql_fetch_array($extract))
    {
    $id=$row['id'];
    $name[$i]=$row['name'];
    $url[$i]=$row['url'];
    $searchurl[$i]=$row['searchurl'];
    $keyword[$i]=$row['keyword'];
    $i++;
    }
    
    function wordsExist(&$string, $words) {
        foreach($words as &$word) {
            if(stripos($string, $word) !== false) {
                return true;
            }
        }
        return false;
    }
    
    function wordExist(&$string, $string) {
            if(stripos($string, $string) !== false) {
                return true;
        }
        return false;
    }
    
    {
    	$keyword = preg_replace('/([a-zA-Z0-9]*(\s){0,1}):/','',$search);
    	$shop = preg_replace('/:([a-zA-Z0-9]*(\s){0,1})/','',$search);
    }
    
    if (wordExist($search, $keyword))
    {
    
    	$redir = "http://domain.com"; //this should be a URL!
    	break;
    	}
    
    elseif (wordsExist($search, array('http://'))) {
        $redir = $search; //this should be a URL!
    }
    
    elseif (wordsExist($search, array('.com','.net','.org','.aero','.asia','.biz','.cat','.coop','.edu','.gov','.info','.int','.jobs','.mil','.mobi','.museum','.name','.pro','.tel','.travel','.ac','.ad','.ae','.af','.ag','.ai','.al','.am','.an','.ao','.aq','.ar','.as','.at','.au','.aw','.ax','.az','.ba','.bb','.bd','.be','.bf','.bg','.bh','.bi','.bj','.bm','.bn','.bo','.br','.bs','.bt','.bv','.bw','.by','.bz','.ca','.cc','.cd','.cf','.cg','.ch','.ci','.ck','.cl','.cm','.cn','.co','.cr','.cu','.cv','.cx','.cy','.cz','.de','.dj','.dk','.dm','.do','.dz','.ec','.ee','.eg','.er','.es','.et','.eu','.fi','.fj','.fk','.fm','.fo','.fr','.ga','.gb','.gd','.ge','.gf','.gg','.gh','.gi','.gl','.gm','.gn','.gp','.gq','.gr','.gs','.gt','.gu','.gw','.gy','.hk','.hm','.hn','.hr','.ht','.hu','.id','.ie','.il','.im','.in','.io','.iq','.ir','.is','.it','.je','.jm','.jo','.jp','.ke','.kg','.kh','.ki','.km','.kn','.kp','.kr','.kw','.ky','.kz','.la','.lb','.lc','.li','.lk','.lr','.ls','.lt','.lu','.lv','.ly','.ma','.mc','.md','.me','.mg','.mh','.mk','.ml','.mm','.mn','.mo','.mp','.mq','.mr','.ms','.mt','.mu','.mv','.mw','.mx','.my','.mz','.na','.nc','.ne','.nf','.ng','.ni','.nl','.no','.np','.nr','.nu','.nz','.om','.pa','.pe','.pf','.pg','.ph','.pk','.pl','.pm','.pn','.pr','.ps','.pt','.pw','.py','.qa','.re','.ro','.rs','.ru','.rw','.sa','.sb','.sc','.sd','.se','.sg','.sh','.si','.sj','.sk','.sl','.sm','.sn','.so','.sr','.st','.su','.sv','.sy','.sz','.tc','.td','.tf','.tg','.th','.tj','.tk','.tl','.tm','.tn','.to','.tp','.tr','.tt','.tv','.tw','.tz','.ua','.ug','.uk','.us','.uy','.uz','.va','.vc','.ve','.vg','.vi','.vn','.vu','.wf','.ws','.ye','.yt','.za','.zm','.zw')))
    {
    	$redir = "http://$search"; //this should be a URL!
    	}
    
    elseif (wordsExist($search, array('tlf:')))
    {
    
    	$redir = "http://opplysning1890.no/index.php?q=$keyword&type=privat"; //this should be a URL!
    	}
    
    elseif (wordsExist($search, array(':')))
    {
    	echo "There is no search engine connected with <b>$shop</b>. If you want to suggest a search engine please <a href=\"suggest.php\">click here</a>. If you rather want to search in Google for <b>$keyword</b>, please  <a href=\"http://www.google.com/search?q=$keyword\">click here</a>.<br />";
    	}
    
    else
    {
        $redir = "http://www.google.com/search?q=$search";
    }
    ?>
    PHP:
    When I submit the form it gives me the following error code:
    Notice: Undefined variable: redir in /home/haukaas/public_html/new/search.php on line 6
    
    Warning: Cannot modify header information - headers already sent by (output started at /home/haukaas/public_html/new/search.php:6) in /home/haukaas/public_html/new/search.php on line 6
    PHP:
    This is where I am stuck :(

    If you want to help me over MSN or Skype let me know and I will send you my info...

    Thanks in advance
     
    misterdh, Jul 7, 2010 IP
  2. misterdh

    misterdh Peon

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    misterdh, Jul 7, 2010 IP
  3. uldtot

    uldtot Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Add my MSN kim @ dicm.dk

    I should be able to fix your errors :)
     
    uldtot, Jul 7, 2010 IP
  4. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #4
    move

    header("Location: $redir"); to the end of the logic (put after "else{ $redir = "http://www.google.com/search?q=$search";}" )

    and delete exit;

    problem is it's trying to call a non set value
     
    themullet, Jul 7, 2010 IP
  5. optimizare

    optimizare Active Member

    Messages:
    507
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #5
    weird. you have redir at the start. You should have it at the end. In a condition. Also, stay away from using header("Location: $redir"); instead of $redir use $_GET["redir"] or $_POST["redir"] depending on how you send redir via posting a form or via get.
     
    optimizare, Jul 7, 2010 IP
  6. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #6
    oh yeah and you should set $search value to the value from your form
     
    themullet, Jul 7, 2010 IP
  7. misterdh

    misterdh Peon

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Problem solved by uldtot

    Thanks alot for your help guys:D
     
    misterdh, Jul 7, 2010 IP
  8. happydev

    happydev Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hey misterdh,

    Please let us know how your issue is solved. It will be a good learning for us :)

    Thanks
     
    happydev, Jul 8, 2010 IP