Load Database Data As dynamic Page - php

How would one go about creating a uri based on the information in the database. I know how I can post to a dynamic php page and have it search the database then use htaccess to rewrite that url. But how do I get php to generate a page based on a URI Link clicked lets say in a menu For example: The database will have a Unique title which will be the unique uri of the portion of the URL, Essentially the title is representative of the link uri in the "a href" link in a menu. So if somesite.com/ loads the main page, but if users clicks the link somesite.com/a-dynamic-page/ which would be a dynamically generated page. How do I get the server instead of throwing a 404 error because the page does not exist, have php look through the database and load the data from the database and then generate the page based on what was found in the database for "a-dynamic-page"<--unique uri in database? Let me know if you need any clarity on this question.

You probably want to first tell .htaccess to process all missing pages with some php script you've written, by putting this inside your .htaccess file:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /my_processor.php [L]
</IfModule>
... which would effectively tell apache that all requests to pages that don't exist, should be redirected to a script called my_processor.php, directly on your website's document root.
Once inside the my_processor.php script, you can look at and parse $_SERVER['REQUEST_URI'], and use that to determine what your script should do next. ie:
[my_processor.php]
<?php
//
#$uri=$_SERVER['REQUEST_URI'];
switch($uri)
{
default: die("Error");
case "/some-path-1/":
SomeFunction1();
break;
case "/some-path-2/":
SomeFunction(2);
break;
}
?>
Please forgive how simple the above is, but hopefully you get the idea.

Related

Adding a RewriteRule for a PHP page with special handle requests

In my php page I have a hyperlink like:
Remove User";
The delete_user_page.php via GET takes the name of the user and executes the script. I want a URL rewrite mechanism inorder to hide the passed variable. So I wrote:
.htaaccess
RewriteEngine On #Turn on the re-writing engine
RewriteRule ^delete/?$ delete_user_page.php?name=$1 [NC,L] # Handle requests for "Delete users page"
When loading this onto the server and refreshing the results nothing has changed/showed so I realized I am doing something wrong.
Where does htcaccess have to be located relative to delete_user_page.php?
Looking at "delete_user_page.php?name=$1", is $1 correct or should it be different?
If there is something else wrong in my script please tell me.

Creating dynamic file in php (not the title)

What I want to do:
Create dynamic php file for each product. What I'm currently doing is, if a person clicks on a products , it opens in one single file with some data i.e. www.example.com/showproduct.php?ref=1085. Here 1085 is a productid & thus it shows the content from DB for the particular id.
Is it possible to show the URL as www.example.com/google-nexus-5-16gb-black.php, where this data comes from DB content?
I don't want to create a page for each product, I think that would be a waste. Is there any solution? You can see even on stackoverflow how it shows the URL for each question.
EDIT
I've used following code in .htaccess file, however it's not rewriting the URL.
RewriteEngine on
RewriteRule ^ref/([A-Za-z0-9-]+)/?$ showproduct.php?ref=$1 [NC]
Any problem with this? And my I will try connecting to DB & get the file name once I know how this works.
You can create a unique alias field for each product record in your DB, and then you can use mod_rewrite to map www.example.com/YOUR-ALIAS to www.example.com/showproduct.php?alias=YOUR-ALIAS. This will give your the desired friendly URL effect.
Update
In your case it appears that you merely want to map google-nexus-5-16gb-black.php to showproduct.php?ref=1085, creating a additional alias field may be overkilled. In this case, you may setup a single rewrite rule:
RewriteEngine on
RewriteRule ^google-nexus-5-16gb-black.php$ /showproduct.php?ref=1085 [L]
.htaccess
URL rewriting file. Redirecting original URL www.example.com/showproduct.php?ref=1085 to www.example.com/google-nexus-5-16gb-black.php
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+).php$ showproduct.php?ref=$1
RewriteRule ^([a-zA-Z0-9-/]+).php/$ showproduct.php?ref=$1
You can read more about Basic URL Rewrite
<?php
include('db.php'); // Connection to your database
if($_GET['ref'])
{
$url=mysql_real_escape_string($_GET['ref']);
$sql=mysql_query("select title from table where ref='$url'");
$count=mysql_num_rows($sql);
$row=mysql_fetch_array($sql);
$title=$row['title'];
}
?>
Probably this link will help you to do what you are looking for

how to hide static pages?

i was given 3 static pages e.g
proposal.test.com/seo
proposal.test.com/ppc
proposal.test.com/design
I checked those directories in the server and there's no dynamic about their indexes, all plain htm file.
the instruction given to me was, hide those url from anyone that doesn't match a random url from database..meaning e.g
if user typed proposal.test.com/seo ,it shouldn't display the page, if the user
typed something like e.g proposal.test.com/seo/a13sdfa and a13sdfa matched a key from a databased, that's the only time the proposal.test.com/seo page will be displayed
so how am I gonna do this in PHP ? because all 3 directories are made up of pure static pages..
i have done the creating of keys already, i just wanna know how to hide these pages by appending a given random key and checking if it does or don't exists in database.
Since the pages are never considered PHP, you can not block the access using PHP.
You can block access by configuring your web server, for example by using a .htaccess file.
If you blocked access the normal way, you can use PHP to allow access to the files on certain conditions..
You should use mod_rewrite (in case of Apache web-server) and setup a rewriting of /a13sdfa into something like ?key=a13sdfa. Also you should include some PHP code in all static files in order to check the key validity.
How about this: move the static files outside the public folder, so they cannot be accessed directly; redirect all requests to a php file (you can use rewrite engine with apache) which will look in the database for the accessed url/key and return the file_get_contents of the corresponding file.
Here's an example of how the .htaccess file could look like:
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /index.php
What this does is the following: if the requested file doesn't exist on the disk (as a file or a directory), it will redirect to /index.php. There you should have the logic to render what page you want.
If you don't know in which variable the server will put the slug, just do a print_r($_SERVER) from inside index.php to find it.
There are 2 ways you could solve this problem.
1) (my prefered) Use .htaccess to only display the page if it matches the regex givin in the .htaccess.
2) In PHP (your actual question) 'Get the slug from the URL, query it to the database and if you get a result display it. Otherwise, send a 404 header from php.
Assuming the following: You have an Apache webserver with mod_rewrite enabled (check php info if you arent sure). Your virtual host allows overriding (AllowOveride All).
.htacces
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+) index.php?check=$1 [QSA,L,t]
If the file or directory exsists on the server it will display the page. So it would display seo, design etc. Otherwise it redirects to index.php and gives its slug as a parameter named $check. With this variable, query to the database, check te result and redirect to the desired page.

Pretty URLs from DB

I am working on creating page links from DB like the following example.
Current page:
www.example.com/page.php?pid=7
In the DB it is saved as title "contact-us" under category "Company Info"
I want it to be like:
www.example.com/company-info/contact-us.html
I have tried different solution and answers but did not got any luck. I am not sure, where will be the PHP part and which rules to write for .htaccess files.
In apache (or .hataccess) do something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /proxy.php?_url=$1 [QSA,L]
So in a nutshell, if the resource being requested doens't exist, redirect it to a proxy.php file. From there $_REQUEST['_url'] will be the url the user was requesting.
Then create proxy.php in your home directory and add whatever logic you'd like to load the correct content.
If you use this from .htaccess, then you may need to add RewriteBase / to your config.
If you want to find this page by url, you will probably do this through php and .htaccess. Make a .htaccess that calls page.php for each and every request. You don't need the pid=7, because, well, how should the .htaccess know it is 7, right? :)
In page.php, you take the original url and split it on the slashes, so you get the category (company-info) and the page itself (contact-us.html). Then, you can look these up in the database. This is in a nutshell how many software works, including Wikipedia (MediaWiki) and CodeIgnitor.
Mind that 'company-info' isn't the same as 'Company Info'. You'll have to specify the url-version in the database to be able to use it for look-up.

display dynamic page using htaccess redirection

i want to redirect
http://testsite.com/campus.php
to
http://testsite.com/campus.php
but the file campus.php doesn't exists. It fetches the page content from database and it must be displayed.
I haven't got any idea.
First of all, your two URLs are the same.
But if you want to call a page that doesn't actually exist but is pulled from the database, you typically redirect some part of the URL to point into a $_GET variable used to access the database. All requests actually go to index.php, and index.php handles the database and displays the correct data.
# Conisde this pseudocode
# Rewrite somepage to index.php?pagename=somepage
RewriteRule /somepage.php /index.php?pagename=somepage
# The actual .htaccess rewrite looks like:
RewriteEngine On
# Assuming pagename is upper/lower letters and numbers only...
RewriteRule /([A-Za-z0-9]+)\.php /index.php?pagename=$1
Now in your PHP, you use $_GET['pagename'] (campus in your case, I think) to call the text from the database and display it.
EDIT I added the \.php to the RewriteRule. Now /campus.php rewrites to /index.php?pagename=campus

Categories