View Full Version : URLS like: index.php?page=page1 ???
mburt
10-19-2006, 03:28 PM
I have links on my page, but instead of linking to pages like:
<a href="guestbook.php">Guestbook</a>
I would like to have
<a href="?page=1">Guestbook</a>
How can I do this?
mwinter
10-19-2006, 03:59 PM
... instead of linking to pages like:
<a href="guestbook.php">Guestbook</a>
I would like to have
<a href="?page=1">Guestbook</a>
How can I do this?
Uh, why would you want to do that? URLs should be transcribable and easy to remember. Ideally, the query string should be used only to modify details about a particular document, not choose the document itself.
Jakob Nielsen has a few guidelines regarding URIs and their importance as a UI feature (http://www.useit.com/alertbox/990321.html), sentiments that are echoed elsewhere.
What exactly makes you think this will be a good idea, either for visitors or yourself?
Mike
mburt
10-19-2006, 04:00 PM
It leaves off the extension, possibly making it easier to navigate.
mburt
10-19-2006, 04:03 PM
So how could I do this?
mwinter
10-19-2006, 04:19 PM
So then why don't you aim for URIs like:
  http://www.example.com/guestbook
Still no "extension", but no unnecessary query string silliness.
I've discussed how to implement extension-less URIs in the past - you even participated in one thread.
Mike
mburt
10-19-2006, 04:23 PM
I've read that post, I couldn't understand it.
So then why don't you aim for URIs like:
http://www.example.com/guestbook
I would... but I don't know how. What's a URI?
costas
10-19-2006, 07:05 PM
If I understood well, you want to use links that have that "?" in the end, like "<a href="login.php?user=username">Login</a>". Well, here's an example:
index.html
<?
$user = $_POST[user];
if(user != NULL)
{
header("Location: members.php?user=$user");
}
?>
....
<body>
<form action="<? echo $_SERVER['PHP_SELF'];?>" method="post">
Username: <input type="text" name="user">
<br><br><input type="submit" value="Login">
</form>
</body>
members.php
<?
$user = $_GET[user]; // you MUST use the "get" method
if($user == "costas") // then verify it
echo "Welcome oh COSTAS!!";
else
echo "You are an unauthorized User!";
?>
This is a very simple example, but I hope you understand how it works. If you want a bit bigger and more complex example, I could give you one I've made!!
Hope I helped!!!
The easiest way to do this is to construct a query-string mechanism as you describe here, then use mod_rewrite or equivalent to map /guestbook to /?page=1.
mburt
10-19-2006, 09:22 PM
Okay.. I think I get it now. Thanks Mwinter, Twey, and costas. PHP is awesome...
djr33
10-19-2006, 09:49 PM
You could, if you're just looking for a nice feel to the url, use page.php?guestbook
Then split the string of the url to work.
list($addr,$page) = explode($_SERVER['REQUEST_URI'],"?",2);
//set the two variables equal to the parts of: URI split at ?, limited to 2 chunks.
echo $page;
//that's "guestbook", etc.This isn't necessarily a great idea, but it might help. If you want simplicity and a single php page as the basis for navigation, could work well.
Note, though, that a major problem is that you can't use GET variables after this. If you wanted to specify something else with the URI, then the & would have to be there, and it would get messy, as that would be in the $page string as well.
You could do some fancy coding, but it would just be a pain.
In that case, you should definitely use ?page=.... instead. It would also make it clearer when making the URLs.
One ?.... value is easy, but after that, using the normal syntax is much clearer.
mburt
10-19-2006, 10:02 PM
I have this on my index.php, and it won't work?
<?php
$page = $_GET[q];
$queries = array("home","scripts","resources","ie","guestbook","about","projects");
$results = array("index.php","scripts.php","resources.php","get_firefox.php","guestbook.php","about.php","projects.php");
for ($i = 0;$i < count($queries);$i++) {
if ($page == $queries[$i]) {
header("Location: $results[$i]");
}
}
?>
and on my menu.php (which is embedded into my index.php using file_get_contents):
<ul>
<li><a href="?q=home" id="index">Home</a></li>
<li><a href="?q=scripts" id="scripts">Scripts</a></li>
<li><a href="?q=resources" id="resources">Resources</a></li>
<li><a href="?q=ie" id="get_firefox">Switch From IE</a></li>
<li><a href="?q=guestbook" id="contact">Guestbook</a></li>
<li><a href="?q=about" id="about">About</a></li>
<li><a href="?q=projects">Upcoming Projects</a></li>
</ul>
<div class="mheader" id="resources">Top 3 Resources</div>
<ul>
<li><a href="http://www.dynamicdrive.com/" target="_blank" title="Dynamic Drive">Dynamic Drive</a></li>
<li><a href="http://www.javascriptsource.com/" target="_blank" title="JavaScript Source">JavaScript Source</a></li>
<li><a href="http://www.scriptsearch.com/" target="_blank" title="Script Search">Script Search</a></li>
</ul>
<div class="mheader">Funpic</div>
<ul>
<li><a href="http://www.funpic.org/">Funpic.org</a></li>
<li><a href="http://www.funpic.org/forum/webhosting">Funpic Board</a></li>
</ul>
djr33
10-19-2006, 10:05 PM
On first glance, looks good.
Give it a test.
Also, you fail to specify an else.
A good php script should always have a default value if someone is trying to hack around your settings, or if an old link points to something that is now invalid, or if the user makes a typo. Otherwise, weird things can happen.
I would suggest removing "index.php" from the arrary, and using:
else {header=index.php} (format properly, of course)
That way, anything that doesn't match will default to the home setting... be it "home", nothing (important as if you just type the page filename, it won't have a value for the get variable), or another non-matching string.
mburt
10-19-2006, 10:07 PM
Try it. It's exactly what I put there:
http://mburt.mb.funpic.org/
Once you go to another page, it doesn't work...
djr33
10-19-2006, 10:12 PM
That's nothing complex.
In your a tags, you use href="?q=....", and that is simply added to the current URL.
So... if you're on scripts.php, then... you get script.php?q=...
Just use <a href="index.php?q=...">
Or, perhaps, <a href="./?q=..."> which I think (but am not sure) would work the same way.
mburt
10-19-2006, 10:12 PM
I just thought of that *knocks self on head. But still one problem. The "index.php?q=scripts" doesn't appear in the address bar.
djr33
10-19-2006, 10:13 PM
huh?
how's that related?
mburt
10-19-2006, 10:15 PM
Look at Twey's site or blm126's site. I'm pretty sure they use the same method, but when I do it, the page address appears in the address bar. Not the query
Actually, there's an easier way. If you specify a get variable without a value, it exists within PHP as an empty string, so you can do:
if(isset($_GET['guestbook']))However, an URL like /guestbook is still better.
mburt
10-19-2006, 11:07 PM
However, and URL like /guestbook is still better.
How would I be able to use such a URL?
djr33
10-19-2006, 11:21 PM
Index page in /guestbook directory.
(Not php)
Look into mod_rewrite with .htaccess.
mburt
10-19-2006, 11:30 PM
My server doesn't allow .htaccess .... Thanks anyway though :p
djr33
10-19-2006, 11:31 PM
At that point, (and even with the php), really... why?
It's not worth it for this.
There are easier ways, and redirecting the browser, even using headers, can't be good practice for no reason.
It's a cool idea... but you're just making extra work for yourself :)
mburt
10-20-2006, 12:06 PM
The arrays, and for-loops were good PHP practice for me anyway :)
costas
10-20-2006, 12:37 PM
Sorry, I only saw the first page!!!
mwinter
10-20-2006, 01:19 PM
I've read that post, I couldn't understand it.
Then ask about it. :) I can't help people understand something if they never say that there's a problem in the first place.
By the way, you're in for a pleasant surprise, later.
What's a URI?
A Uniform Resource Identifier. URIs are a superset of URLs (Uniform Resource Locator) and URNs (Uniform Resource Name). That is, all URLs are URIs.
A Locator not only identifies a resource, but also indicated how to access it by specifying its location (for example, its network address)
However, and URL like /guestbook is still better.
How would I be able to use such a URL?
Index page in /guestbook directory.
Again (I mentioned this in the thread I cited), the URI for such a thing would end in a slash:
  http://www.example.com/guestbook/
Using a separate directory for every single document isn't practical.
My server doesn't allow .htaccess
Thankfully, and here's the surprise, it doesn't need to. In your case, it would seem that the MultiViews option (one of the content negotiation mechanisms I discussed) is already enabled.
Just as a random example, consider your Basic Calendar script (you misspelt the heading, by the way). The URL for that ends:
  /scripts/basic_calendar_root.htm (http://mburt.mb.funpic.org/scripts/basic_calendar_root.htm)
However, it can also be accessed using:
  /scripts/basic_calendar_root (http://mburt.mb.funpic.org/scripts/basic_calendar_root)
Why? When the request is received, no file, redirect rule, or anything else is found. However, because MultiViews is enabled, the server searches for files that begin with that name. When it finds basic_calendar_root.htm, it checks to see what type it is (text/html), and whether the browser making the request will accept it (it will). As a result, it returns that same file.
If there are multiple matches, the server negotiates between them, finding the best match for the client based on language, encoding, type, etc. This allows for multiple representations that vary based on filename, using the name to distinguish one from another (for example: index.html.en, text/html, English; index.html.fr, text/html, French).
The same thing can apply to your PHP files (assuming that they really need to be PHP, now): /resources.php (http://mburt.mb.funpic.org/resources.php) can be accessed as /resources (http://mburt.mb.funpic.org/resources).
Unfortunately, your host has configured PHP in the unfriendly way by MIME type, rather than handler, but it shouldn't be a problem. Moreover, with scripts.php, using the URL ending /scripts would result in a redirect to the directory with the same name. You'd either have to change the directory name or place an index file in that directory.
Mike
mburt
10-20-2006, 01:27 PM
Holy crap... how did that happen? When you type in the url for mburt.mb.funpic.org/resources, it automatically went there... you're a complete genius. The process of this did advance my PHP a little more though. But again, thanks for all the help :)
mwinter
10-20-2006, 01:36 PM
Holy crap... how did that happen?
You're host enabled the MultiViews option. It's actually part of the default configuration for user directories (http://www.example.com/~username/). Either that directory structure is used internally, or they enabled it for all virtual hosts.
I thought I'd give it a shot and tried it. :cool:
By the way, you might want to re-read the last paragraph of my post as you might have been reading it whilst I was editing.
Mike
mburt
10-20-2006, 01:44 PM
Yes, I changed scripts.php to scripts_page.php. I realized it was already a directory, and that it would take me to the index of the directory. Thanks.
motormichael12
11-10-2006, 09:31 PM
Construct the page in html and then use this and change/add to your taste:
<?php
//Copy and paste the HTML that is the same in all your pages (the header and footer) into new files and name them header.inc and footer.inc.
//They will be included at the complete top and bottom of your pages, so you will only have to edit these 2 files if you want to change your layout.
include('header.inc');
if(!$_SERVER['QUERY_STRING']) { ?>
//Paste here the HTML coding you have on your main page. This is what people will see when they go to index.php, without any ?section bit after it.
<? } elseif ($_SERVER['QUERY_STRING'] == "about") { ?>
//Paste here your HTML code on the About page (without the header and footer code, obviously).
<? } elseif ($_SERVER['QUERY_STRING'] == "rules") { ?>
//Paste here your HTML code on the Rules page (without the header and footer code, obviously).
<? } elseif ($_SERVER['QUERY_STRING'] == "join") { ?>
//Paste here your HTML code on the Join page (without the header and footer code, obviously).
<? } elseif ($_SERVER['QUERY_STRING'] == "codes") { ?>
//Paste here your HTML code on the Codes page (without the header and footer code, obviously).
<? } elseif ($_SERVER['QUERY_STRING'] == "members") { ?>
//Paste here your HTML code on the Members page (without the header and footer code, obviously).
<? } elseif ($_SERVER['QUERY_STRING'] == "extra") { ?>
//Paste here your HTML code on the Extra page (without the header and footer code, obviously).
<? }
include('footer.inc'); ?>
Whatever you save this as is what they will be. If you save this script as "index.php", you will have "index.php?about", "index.php?rules", "index.php?join", etc.
You can also change some of the code to make it so it is like "index.php?act=about" by doing the following:
"elseif ($_SERVER['QUERY_STRING']" or "if(!$_SERVER['QUERY_STRING']" can be changed by replacing "SERVER" with "GET" and "QUERY_STRING" to what you want it to be after the "?"...
e.g.
($_GET['act'] == "about") on "index.php" will make the url appear as "index.php?act=about"
On forum boards like this they do that... this page is "showthread.php?t=14133", they used "t" as "thread"
Strangeplant
11-14-2006, 03:12 PM
Ah! But there is another method (the one that I like the best.) Use Ajax to take the 'page1' from either a .txt or .html file and dump it in the index.html (or index.php) page. That way, you can use the menu inside index.html to load additional pages and whatever page you want can be called from anyother page, displaying the content inside index.html. index.html can have a default content as well. There was a previous post where this was all worked out. I use it occasionally on several websites where 'non-coders' can change their content often without messing the website up, and where email content can be displayed on a page, directory listings, image galleries, etc.
Ugh, no. The PHP response was a far better idea. It neither messes up browser functionality nor requires Javascript to view the page.
Also, if using AJAX, one would presumably be dumping XML to the page, not HTML. This could result in some odd goings-on.
Strangeplant
11-15-2006, 03:41 PM
Ok, whatever suits your particular needs.
Ajax is simpler and doesn't require server mods, all the folders, easier to test, change and faster to implement, format to page, etc. And I got the basic routines from DynamicDrive scripts. BTW, both .html and .txt files work perfectly because the server dishes them up as html.
.txt files work perfectly because the server dishes them up as html.Then that server is misconfigured (or at least, configured in an unconventional way), and this shouldn't be expected to be the case on all servers. Plain-text files (which is what the extension .txt usually signifies) should be served as text/plain, and they shouldn't be parsable by an XML parser whether text/plain or text/html.
Ajax is simpler and doesn't require server modsAJAX is far from simple (in terms of the amount of code required to achieve the same effect). Relying on a server-side technology is considered greatly preferable to relying on a client-side one, since the server can be guaranteed to have the technology installed, whereas what the client supports will vary from client to client (and occasionally, even request to request).
faster to implementIn this case, it would be much more difficult: assuming no server-side technologies, one would have to use Javascript to render the XML into HTML which can be dumped to the page.
I got the basic routines from DynamicDrive scripts.There are (as far as I know) no AJAX (Asynchronous Javascript And XML) scripts on DynamicDrive except the RSS feeds, which rely upon a server-side technology.
djr33
11-16-2006, 09:10 AM
Basic AJAX routine:
http://www.dynamicdrive.com/dynamicindex17/ajaxroutine.htm
Oh, there is one :) I didn't see that.
motormichael12
11-17-2006, 09:57 PM
okay you can use ajax but just a warning... if you want to use the one I posted previously then you need to save it as a .php file, make sure its php and not .htm or .html or anyhting else.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.