Output Webpage (Variable URL) with Database values, using PHP Template - php

I did some searching around, but I'm not very experienced in PHP, so I don't know the correct Terms/Words I am looking for.
However, I do have a good idea of what I want to do.
I have a website with many images, each image has a database entry (MySQL).
I would like each image to have its own landing page, based on its database values. (Title, Category, Tags, etc...)
I don't want to make a separate .php file on my server for each image. (Eg: pinkflowers.php, redroses.php, etc...)
There should be 1 PHP template file, that outputs the webpage for ALL images, based on the URL (variable) that the user visits.
So if someone visits "mysite.com/pinkflowers.php", the page should be output with the variables of pinkflowers from my database.
However, the file pinkflowers.php doesn't actually exist, only template.php exists, which would be the "blueprint" for all the images in my database.
I would like the .php to be removed from the URL in the browser.
"mysite.com/pinkflowers.php" => "mysite.com/pinkflowers"
I already have code that does this with my existing pages (below); I'm not sure if it will also work with these "imaginary" pages.
(.htaccess)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
If the user tries to visit a page that has no related database entry (eg: "mysite.com/430jfif0ij"), they should be re-directed to the homepage.
I'm not expecting anyone to give me ALL the answers, but please at least guide me in the right direction to begin making such a template.
Thanks!

I was able to solve my own question eventually, in the mean time that no Answers were given.
I followed this Tutorial here:
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/
My .htaccess contained:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^category1/(.*)$ ./template.php?image=$1
RewriteRule ^category2/(.*)$ ./template.php?image=$1
RewriteRule ^category3/(.*)$ ./template.php?image=$1
etc...
category# - being the "folders" that my images are categorized by.
I did this on the template.php to extract the Variable from the URL:
$result = mysql_query('SELECT * FROM image_list WHERE image_name="' . mysql_real_escape_string($_GET['image']) . '"');
To redirect the visitor to the Homepage if they enter an invalid URL/ID, I used this:
if (mysql_num_rows($result)==0) {
echo "<script>window.location = 'http://mysite.com/'</script>";
}
So the combination of all these methods achieved exactly what I was looking for.

Let your .htaccess be something like:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ template.php/$1
Then (on template.php) use $_SERVER["path_info"] to determine the path the user has entered (i.e. /pinkflowers) and query the database accordingly.
The reason I like $_SERVER["path_info"] is because it doesn't use (or pollute) the GET variable space.

Related

Imitate the page creation/url rewriting effect of this strawpoll.me

I am looking to do something along the lines of what is shown here:
http://strawpoll.me
When you create a poll on there it gives you a unique url to which you and anyone else can access it by visiting that url.
Now obviously they can't be cluttering up their server with thousands of individual files containing the polls and I'm assuming they have one basic template to which they load the poll information from a database.
I want to make a form and when the user submits it they get their own unique link like how it is on the strawpoll website.
As is it is a simple dynamic page creation requiring only few files...
The poll creation page which is static
The processing.php file which collects the data and inserts into the db
The dynamic poll rendering page polls.php which renders the poll based on an auto-incremented id assigned to the poll.
The polls-results.php which returns the results.
http://thepollsite.com/12345
.htaccess is used to identify which poll to render as well as which results to render.
# Turns on Mod-Rewrite Engine
# ----------------------------------------------------------------
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
# Poll Pages
# ----------------------------------------------------------------
RewriteRule ^([^/]+)/?$ polls.php?id=$1 [QSA,L]
RewriteRule ^([^/]+)/?$ polls-results.php?id=$1 [QSA,L]
On both polls.php and polls-results.php you get the id with: $poll_id = $_REQUEST['id']; and process from there.
The embed code is rendered in a simple dialog pop which provides the iframe code as shown.
Though there are frills you can add... its is really a simple process.
On the .htaccess side, you'll want something like:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
With that in place, all requests for files/directories that don't already exist on the server will run index.php instead of showing a 404 page. Then, to get the requested path, use $_SERVER['PATH_INFO'] in your index.php.
This is the general method of using "clean" URLs with PHP. It's probably already been said in another SO answer, but it's hard to find so I wrote it again.
Related:
Why use a single index.php page for entire site? and How to do URL re-writing in PHP?

From trailing slash to query (PHP/.htaccess)

I would like to keep the links in the address bar as short and clean as possible, for example:
http://example.com/th1ng
If in the database there is a row with 'th1ng' as username, the above link should work like
http://example.com/user.php?name=th1ng
while still showing the first, clean link in the address bar.
If the user doesn't exist, it shows/redirects to the 404 page.
Also, there are other files and directories that will need to be access via the trailing slash too. And it is possible there is a GET query on top as well, like
http://example.com/th1ng?img=13805
which should act like
http://example.com/user.php?name=th1ng?img=13805
I got all of the PHP stuff, including the database check and redirecting to the 404 in case the user doesn't exist.
But.. what would be the proper way to get around showing the clean link in the address bar? With other files and directories on the server still being accessible as well. I can only assume some .htaccess needs to be used but I don't see how exactly.
Thanks for the help!
You will need to use mod_rewrite for that. This would then be placed in the .htaccess.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ user.php?name=$1 [QSA,L]

.htaccess redirect when running a Proxy Script I need just certain URL's redirected to a DMCA takedown page

I've run into a problem I just cant solve that is:
I have a proxy script running on FileHound.co.uk
The htaccess file contains:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /?load=%{REQUEST_URI} [L,QSA]
I need to be able to redirect certain URL such as this:
filehound.co.uk/?load=/torrent/9392320/%5Ba-destiny%5D_toriko_-_135_(1280x720_hi10p_aac)_%5Bde83bd3d%5D.mkv
I want to prevent the link from displaying its destination instead being redirected to a page to i.e. /DMCA/DMCA.html
There are many links that need redirecting in a similar way. As I say its a proxy script and so I cant remove the page or material myself.
Any help would be much appreciated...
If you put all the links that need to be redirected in a database then you can query that table and redirect if it exists something like this (pseudo code):
# if (url in db)
# redirect /DMCA/DMCA.html
Don't forget to sanitize your input, don't want sql injection ;)

Apache presenting virtual subdirectories and passing parameters to PHP script

My root directory contains a selection of PHP files: index.php, about.php etc. Each of these can take an ID parameter to customize a few of the variables on the page, such as contact phone number, email address etc.
If a visitor was to access http://myserver/joebloggs (joebloggs being a parameter, not a subdirectory), I want them to be served up with the index.php page from the root directory, but for joebloggs to be passed to index.php as the ID and the page then customized. I don't want to have to create a subdirectory for every user ID, I'd rather maintain this info in a DB and have PHP generate the pages for me.
Furthermore, when the user then navigates to the about.php page, I would like this ID to be carried over and then have the about page customized too, i.e. a link to 'about' maintains the ID parameter in the URL: http://myserver/joebloggs/about/
I've got a rough idea in my mind how to do this and have been reading up on mod_rewrite but haven't had much luck in piecing various solutions together.
Any pointers or help would be much appreciated.
Try something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} \/(.+?)\/about
RewriteRule . /about.php?p1=%1 [L]
RewriteCond %{REQUEST_URI} \/.*?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)$ /index.php?p1=$1 [L]
Should do the job. Tested it on my server.

Mod_Rewrite Query with Root Domain

I'm trying to do a query on some information if someone types, for example:
http://domain.com/abracadabra
If someone enters in this address, I want it to go to:
search.php?query=abracadabra
I've done rewrites with queries before, but not from the root.
Any help would be great!
Would it be possible to check if the string being searched is numerical and send it to a different PHP page? Maybe link to a directory with a new .htaccess?
Doing it from the root is no different than doing it anywhere else. The below will turn every request for a resource (that doesn't exist in the file system) to search.php.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ search.php?query=$1 [L,QSA]

Categories