Jump to content

  • Log In with Google      Sign In   
  • Create Account

How to have index.php load different content


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 pintee   Members   -  Reputation: 131

Like
0Likes
Like

Posted 30 April 2013 - 04:09 PM

Hi everyone,

 

I would like to have my website display different content while remaining on the same index.php page. The functionality I am after is demonstrated by www.sryth.com. If you sign up for an account, login, and start a new game, you will see what I mean. The address stays the same throughout the game(i.e. www.sryth.com/game/index.php), even as new content is being displayed. How do I achieve this effect??



Sponsor:

#2 Prozak   Members   -  Reputation: 869

Like
0Likes
Like

Posted 30 April 2013 - 05:41 PM

Look into mod-rewrite.

 

mod-rewrite allows you to redirect the content into the same php handler, index.php for example, and you pass it the URL.

 

This is the basis for the MVC model.

Hopefully this link will get you started fast:
http://net.tutsplus.com/tutorials/other/a-deeper-look-at-mod_rewrite-for-apache/

 

Good luck!



#3 MichaelNIII   Members   -  Reputation: 195

Like
0Likes
Like

Posted 30 April 2013 - 06:05 PM

I was just working on this for a simple database for my work. I used the get method and sent a page name to my index page, and then have a switch statement to load each page the user will need to have access to using the include function. If you don't want the ?page= in your url, you can use the post method instead. Hope that helps some.

#4 slicer4ever   Crossbones+   -  Reputation: 3888

Like
0Likes
Like

Posted 30 April 2013 - 06:28 PM

http://www.w3schools.com/xml/xml_http.asp

 

you basically make a request to another php page in the background, and it responds with a plain-text response(exactly as if a user had accessed the page with their browser), you can attach post/get data and have the php on that page handle the input, and generate a response.  then use javascript to modify the user's page with this new data.


Edited by slicer4ever, 30 April 2013 - 06:30 PM.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#5 pintee   Members   -  Reputation: 131

Like
0Likes
Like

Posted 30 April 2013 - 06:45 PM

sorry michael, my php knowledge is rudimentary. I thought get and post methods were only if a form is involved. What if its just a page with links, and when you click on a link it remains as index.php but with different content? Thanks for your time.



#6 MichaelNIII   Members   -  Reputation: 195

Like
0Likes
Like

Posted 01 May 2013 - 02:23 AM

The get method doesn't require a new form. It just requires the data be correctly added to the url. I use links that look like www.mysite.com/index.php?page=home. I am also fairly crude with my php aswell though. I wanted this so I could use it to make the nav bar really simple.

#7 Andy474   Members   -  Reputation: 685

Like
1Likes
Like

Posted 01 May 2013 - 02:36 AM

i think you can get away with

 

<?php include "someGenericButtons.html"; ?>
<?php
var $navigationBarToShow = GET['nav']; //Get Navigation from header (So 'TOPLEVEL' or something)
 
if($navigationBarToShow == 'TOPLEVEL')
    include "Navigation/ToplevelNavigation.html";
?>

 

then, in your navigation bar, your buttons simply point at

 

<a href="index.php?Nav=Button1">Button 1</a>

 

or Similar, My web knowledge isn't great, but this is how i did my 2nd Year Uni Web Project.

 

EDIT: Note this is Semi-psudo code :P


Edited by Andy474, 01 May 2013 - 02:40 AM.


#8 pintee   Members   -  Reputation: 131

Like
0Likes
Like

Posted 01 May 2013 - 04:32 AM

I've been experimenting with www.sryth.com in order to glean how it might be reverse-engineered.

* Firstly I login and start off from www.sryth.com/game/index.php. (this has content on it like text and links)

* Secondly, I open a link on the page in a new window. This brings up the address http://www.sryth.com...ioussectionid=1

for a split second before redirecting to the address http://www.sryth.com...f_c=parser2.inc (this page also has content on it like text and links).

Any guesses as to the implementation from what I have just described?

#9 wicked357   Members   -  Reputation: 1165

Like
0Likes
Like

Posted 01 May 2013 - 11:07 AM

To add to Andy474... You can do the same with .php files if you want he content to be dynamic from a database. I just add modrewrite to my htaccess file and it is no problem.

 

So instead of going to www.example.com/index.php?somepage=blah&somethingelse=something

 

using modrewrite you can handle links to be like,

 

www.example.com/blah/something to clean it up...

 

have the index.php file handle the $_GET[somepage] and $_GET[somethingelse]

 

then as Andy474 stated do include "blah/something.php"

 

You can of course change that however you like and allow for a lot more parameters if you need to. Hope this helps a little.



#10 Michael Tanczos   Senior Staff   -  Reputation: 5217

Like
2Likes
Like

Posted 01 May 2013 - 01:35 PM

eek.. I think what you want to look into is a combination of jQuery and ajax.   jQuery has all the capabilities on the browser end you need, including a way to update the history and append hashbangs to urls so users can still use the back button with their browser.   Instead of an xmlhttprequest call directly you're better off using a mature library like jquery.

 

Maybe start with looking at something like this:  http://iviewsource.com/codingtutorials/learning-how-to-use-jquery-ajax-with-php-video-tutorial/

 

An ajax example on our site is when you click "Top Members" and start typing a username.. no page reloads to generate the content on the members page.


Edited by Michael Tanczos, 01 May 2013 - 01:37 PM.


#11 MichaelNIII   Members   -  Reputation: 195

Like
0Likes
Like

Posted 01 May 2013 - 05:33 PM

Indeed, sorry I couldnt give decent example code, I was on a phone. Andy's method is what I was refering to.

 

My index page loading looks like

 

if     ($Page == 'home')        {include('MainPage.html');}
else if($Page == 'login')       {include('LoginPage.html');}
else if($Page == 'loginsuccess'){include('LoginSuccess.php');}
else if($Page == 'logout')      {include('Logout.php');}
else                             {include('MainPage.html');}
 

for displaying the different pages, and <li><a href=\"Index.php?Page=login\">Login</a></li> for the different navagation bars. This is a really poor example and could be inproved on a lot, but Eh maybe you will find it more userful then just saying steps, my syntax is ugly as hell but I tend not to worry about my syntax rules until my 2nd or 3rd rewrite for sometime when its less "lets try and get this to work right" and more "lets try and make this modifyable, work right, and easy to read"

 

<?php
session_start();

echo "<div id=\"navbar\">
<ul>

<li><a href=\"Index.php?Page=home\">Home</a></li>";

if($_SESSION['myusername']==''){echo

"<li><a href=\"Index.php?Page=signup\">Signup</a></li>
<li><a href=\"Index.php?Page=login\">Login</a></li>";}

else{echo
"<li><a href=\"Index.php?Page=logout\">Logout</a></li>";}

echo"
</ul>
</div>
<div id=\"main\">";


echo $_SESSION['myusername'];
echo "<br>";
$Page = $_GET['Page'];

if     ($Page == 'home')        {include('MainPage.html');}

else if($Page == 'login')       {include('LoginPage.html');}
else if($Page == 'loginsuccess'){include('LoginSuccess.php');}
else if($Page == 'logout')      {include('Logout.php');}

else                             {include('MainPage.html');}



echo"</div>";



?>





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS