Bengau
Bengau

Reputation: 175

PHP search url request

I need some help with this I have for example index.php and and i need to make it something like. someone access:

index.php?search=blbla 
include search.php
else
include home.php

I need an advice with this thanks

Upvotes: 3

Views: 329

Answers (7)

batfastad
batfastad

Reputation: 2013

When using isset() you need to be aware that with an empty GET variable like this script.php?foo= that isset($_GET['foo']) will return TRUE
Foo is set but has no value.

So if you want to make sure that a GET variable has a value you might want to use strlen() combined with trim() instead...

if (strlen(trim($_GET['search'])) > 0) {
    include('search.php');
} else {
    include('home.php');
}

Also you might want to use require() instead of include(). A PHP fatal error is generated if search.php cannot be "required" with just a PHP warning if search.php cannot be "included".

Upvotes: 0

Chris
Chris

Reputation: 5605

<?php

//if($_GET['search'] > ""){ // this will likely cause an error
if(isset($_GET['search']) && trim($_GET['search']) > ""){ // this is better
   include ('search.php');
}else{
   include ('home.php');
}

?>

Upvotes: 0

AboQutiesh
AboQutiesh

Reputation: 1716

use it like this

if (isset($_GET['search']))
  include 'search.php';
else
 include 'home.php';

Upvotes: 0

faq
faq

Reputation: 3076

$sq = $_GET['search']; //$_GET['']
if (isset($sq) && $sq != '') {
include('search.php');
} else {
include('home.php');
}

Upvotes: 2

Sammaye
Sammaye

Reputation: 43884

I personally prefer to check if a $_GET is set and if it actually equals something like so:

if(isset($_GET['search']) && strlen(trim($_GET['search'])) > 0): include 'search.php'; 
else: include 'home.php';

This will avoid the problem of putting in the $_GET variable but not actually setting it.

Upvotes: 0

Thomas Clayson
Thomas Clayson

Reputation: 29925

Well, you could use isset() to see if the variable is set. e.g.

if(isset($_GET['search'])){
    include "search.php";
}
else {
    include "home.php";
}

Upvotes: 2

Miroslav
Miroslav

Reputation: 1960

Try this

if (isset($_GET['search'])) include('search.php');
else include('home.php');

Upvotes: 2

Related Questions