0

I don't know how to explain this, but I will try my best to explain it.

I am developing my website in php. So I have created an index.php file in Search folder

So if I have to search any word like 'good', it's address is

http://127.0.0.1/Website/search/?search=dh

Now if I try to access

http://127.0.0.1/Website/search/

I get an error on my page

Undefined index: search

because it is unable to find my $_GET["search"] variable which I am using to get the value of the input type.

Is there any way by which I can make this url

http://127.0.0.1/Website/search/something

be interpreted as

http://127.0.0.1/Website/search/?search=something

4 Answers4

2
Undefined index: search

"Because it is unable to find my $_GET["search"]" : Then check if the variable exists :

if (isset($_GET['search'])) {
    // use $_GET['search'];
}

For your second question, you can have an URL like "mysite.com/Website/search/something" using URL rewriting with htaccess. Search these terms on SO or Google and you will find a lot of good tutorials.

Clément Malet
  • 5,062
  • 3
  • 29
  • 48
2

For first problem: use isset() to check if $_GET['search'] is defined before using it.

For Second question: use a .htaccess file with the rule

RewriteRule ^/search/(.+)$ /search/?search=$1 [L,R] 
webNeat
  • 2,768
  • 1
  • 20
  • 22
1

You're getting the:

Undefined index: search 

error because you don't check if the variable exists.

To do this you can do a simple if statement like this one down below:

if(isset($_GET['search'])) {
    // search index exists
}

For changing your URL you need to use URL rewriting, here is a good article about it: http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

Jan-Willem de Boer
  • 797
  • 10
  • 19
0

You should use rewrite rules in apache: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html Than in Your script drop part of code reading "search" param and read $_SERVER['REQUEST_URI'] , parse it to get through the "seach" part in URL and read whats after it.