Search code examples
phpsearchsearch-engine

Php redirection of a form with search query term between url


I'm using PHP in order to redirect some search query. There is some example of code here (click).

And my PHP code is:

audio_action.php :

<?php
$search_field = trim($_POST['audio_field']);
$search_engine = trim($_POST['audio']);
$url_params = preg_replace('/(\ )+/', '+', $search_field);
$url = array('deezer'=>'http://www.deezer.com/s.php?s=', 'jiwa'=>'http://www.jiwa.fm/#search/track/', 'last.fm'=>'http://www.last.fm/music?q=');
header('Location:'.$url[$_POST['audio']].$url_params)
?>

and video_action.php :

<?php
$search_field = trim($_POST['video_field']);
$search_engine = trim($_POST['video']);
$url_params = preg_replace('/(\ )+/', '+', $search_field);
$url = array('youtube'=>'http://www.youtube.com/results?search_type=&amp;search_query=', 'dailymotion'=>'http://www.dailymotion.com/relevance/search/', 'google_video'=>'http://video.google.com/videosearch?q=');
header('Location:'.$url[$_POST['video']].$url_params)
?>

The problem is that I can't use it when search terms must be in the middle of an url.

For example, for Jiwa it should be:

http://www.jiwa.fm/#search/track/{%22q%22%3A%22keywords%22}

Where "keywords" is the place where keywords should be.

And without those %22} characters search doesn't work.

So how to improve this PHP code in order to make it work with such query?

Somebody told me too that

$search_engine = trim($_POST['video']);

is useless, but when I remove it, it doesn't work anymore.

I'm currently using video_action.php for video search and audio_action.php for audio but if you find some way to merge those file into a single one while keeping 2 forms in my HTML code it would be awesome.

Please help me improve this code.

PS: I don't want to use JavaScript for this.


Solution

  • Try this:

    <?php
    if (!empty($_REQUEST['audio_field']))
    {
        $url = array(
            'deezer'=>'http://www.deezer.com/s.php?s=__keywords__',
            'jiwa'=>'http://www.jiwa.fm/#search/track/{%22q%22%3A%22__keywords__%22}',
            'last.fm'=>'http://www.last.fm/music?q=__keywords__');
    
        header('Location:'.str_replace('__keywords__',preg_replace('/(\ )+/', '+', trim($_REQUEST['audio_field'])),$url[trim($_REQUEST['audio'])]));
        die();
    }
    else if (!empty($_REQUEST['video_field']))
    {
        $url = array(
            'youtube'=>'http://www.youtube.com/results?search_query=__keywords__',
            'dailymotion'=>'http://www.dailymotion.com/relevance/search/__keywords__',
            'google_video'=>'http://video.google.com/videosearch?q=__keywords__');
    
        header('Location:'.str_replace('__keywords__',preg_replace('/(\ )+/', '+', trim($_REQUEST['video_field'])),$url[trim($_REQUEST['video'])]));
        die();
    }
    else
    {
        // No search query; redirect to search page
        header('Location:http://lostsymphonia.free.fr/r/index.html');
        die();
    }
    ?>
    

    Notes:

    1. The two PHP files can now be merged.
    2. Don't use "&amp;" in a URL - that's only for HTML. Location: is an HTTP header, which is not HTML.
    3. Use method="get" instead of method="post" - searching is an idempotent action.
    4. You accidentally used $url[$_POST['audio']] instead of $url[$search_engine] - you never even use $search_engine; how could removing it possibly make a difference?