Jump to content

Url $_GET


renfley

Recommended Posts

K hey guys hoping one of you can help me out lol,

 

Problem=

When someone clicks a link it directs them to index.php?page=home

<ul>

      <li><a href="index.php?page=home">Home</a></li>

      <li><a href="index.php?page=about">About us</a></li>

      <li><a href="index.php?page=contact">Contact us</a></li>

</ul>

 

What im trying to achieve is since it the same page i wanna display different content based on the page= ?

 

i was wondering if someone could write something quick basicly

 

<?php

          (verify to see if there is something after index.php ex: ?page=home)

        if there isnt

            echo 'welcome to my site';

        and if there is somethign there

          echo 'include/page/contact.php';

?>

 

Srry if this sound messed up lol

Link to comment
https://forums.phpfreaks.com/topic/193314-url-_get/
Share on other sites

u can actually do one thing change that to index_new.php

and then in the index_new.php

u can write something like this

<?php
include("index.php");
?>

 

so what it will do is that when ever some one cliks on the link it will send the request to index_new.php file and that in turn pass the values back to the index.php

 

so in the top of index page u can write something like this

 

$Page=$_GET['page'];

Link to comment
https://forums.phpfreaks.com/topic/193314-url-_get/#findComment-1017857
Share on other sites

actually you shouldnt really do that because then the user could set the url as http://www.example.com/index.php?page=phpfreaksisawesome and it wouldnt load the index even tho thats not a real page

 

Try:

<?php

$page = $_REQUEST['page'];

if ($page == "contact")
{
require('contact page');
}
else if ($page == "about")
{
require('about page');
}
else if (($page == "home") || (!$page))
{
// your code for the home page
}

?>

Link to comment
https://forums.phpfreaks.com/topic/193314-url-_get/#findComment-1017869
Share on other sites

actually you shouldnt really do that because then the user could set the url as http://www.example.com/index.php?page=phpfreaksisawesome and it wouldnt load the index even tho thats not a real page

 

Try:

<?php

$page = $_REQUEST['page'];

if ($page == "contact")
{
require('contact page');
}
else if ($page == "about")
{
require('about page');
}
else if (($page == "home") || (!$page))
{
// your code for the home page
}

?>

 

yeah...good point !

Link to comment
https://forums.phpfreaks.com/topic/193314-url-_get/#findComment-1017877
Share on other sites

Its better to use a switch statement tho

<?php

switch($_GET['page']){
case 'home':
// include or whatever on home
break;
case 'news':
// include or whatever on news
break;
default:
// include or whatever when $_GET['page'] doesnt match other case, or dont exist
}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/193314-url-_get/#findComment-1017881
Share on other sites

ALPINE You got the ball lol

 

so ive changed this

 

<?php

 

$page = $_REQUEST['page'];

 

if ($page == "home")

{

include ('pages/home.php');

}

else if ($page == "products")

{

include ('pages/products.php');

}

else if ($page == "about")

{

include ('pages/about.php');

}

else if ($page == "order")

{

include ('pages/order.php');

}

else if ($page == "contact")

{

include ('pages/contact.php');

}

else if (($page == "home") || (!$page))

{

// your code for the home page

echo 'welcome home';

}

else

{

echo 'hello';

}

 

 

?>

 

The next issue is that because when the page loads its only index.php and not (index.php?page=home)

It give me

Notice: Undefined index: page in C:\wamp\www\Store Front\index.php on line 66

welcome home

 

Any suggestions

Link to comment
https://forums.phpfreaks.com/topic/193314-url-_get/#findComment-1017908
Share on other sites

If you are wondering found a quick solution that will save on code space lol

 

<?php

$page = $_GET['page'];

if ($page)

{

include ("include/".$page.".php");

}

else

{

echo 'Welcome to home page';

}

?>

 

Only problem is the original

Notice: Undefined index: page in C:\wamp\www\Store Front\index.php on line 66

Welcome to home page

Link to comment
https://forums.phpfreaks.com/topic/193314-url-_get/#findComment-1017916
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.