clean url using php [duplicate] - php

This question already has answers here:
Reference: mod_rewrite, URL rewriting and "pretty links" explained
(5 answers)
Closed 8 years ago.
ive seen things like
<?php
if(is_array($_GET)) {
$page = isset($_GET['p'])?strtolower(urlencode($_GET['p'])):'index.php';
$page = preg_replace('/[^A-Za-z0-9]/','',$page);
// echo '['.$page.']';
}
else $page = '';
if(!file_exists($page.'.php')) require("index.php");
else require(_path_.$page.'.php');
}
?>
But i dont really get how this works entirely.
Basically I want to do something with php and htaccess i believe i will know, example; domain.com/page/about would go to domain.com/pages/about.php and output the contents? i know ive seen this somewhere but i forget what its called! I've been searching forever now!
Im not the best at code and need a bit help to do, please.

I think what you're looking for is mod_rewrite. See Apache's documentation for a comprehensive explanation. Tuts+ also has a nice tutorial.
You essentially want to get the the page as a token and pass that to this script as a GET parameter, which your script will evaluate.
If you don't need the full functionality of mod_rewrite and you're just looking to drop the PHP extension, then this question is more relevant: .htaccess; no extension required

Related

Getting A PHP Variable From The URL (Without A ?) [duplicate]

This question already has answers here:
URL rewriting with PHP
(5 answers)
Closed 7 years ago.
Perhaps I'm using the wrong search terms to try to find this online, but I am trying to accomplish the task of passing a variable in a URL path, with using an identifier.
For example, here is my current URL: http://www.myurl.com/test/index.php?name=bob
On my index.php page, I would set something along the lines of $name = $_GET['name']; and have no issue using this variable.
My goal, however, would be to use the URL: http://www.myurl/test/bob/ and still be able to receive "bob" as the name variable in my script.
Is this possible, hypothetically? Thank you!
put in your htaccess a mod_rewrite statement like
RewriteRule ^test\/([a-z]+)\/?$ index.php?name=$1
One of the easiest ways to do this (and also a bit more secure) is instead of using a GET statement, using a session variable. You could change the url to be whatever you like using mod_rewrite as you have suggested, however you can still access the variable without anything special.
For instance, you just start your session like so
session_start();
then set your session variable, like so (assuming you have already defined $name):
$_SESSION['name'] = $name;
and then on the page where you'd want to get name, put session_start(); at the top of the page, and then, instead of $_GET['name'] just call the variable as $_SESSION['name'] instead.
This way you don't really need to worry about using the URL for passing the variable from one page to another. It won't be affected by rewriting.
Of course, your other option, if you wish to continue using it as a GET variable is this: https://stackoverflow.com/a/8228851/3044080

Read current URL or URL variable in joomla [duplicate]

This question already has answers here:
How to get full url of a article by it's ID in joomla?
(3 answers)
Closed 8 years ago.
I'm using joomla 2.5. I'm trying to read URL variable OR full URL in Joomla component customization.
I'm able to read URL like:
http://xyz/index.php?option=com_xyz&format=raw&tmpl=component&lang=en
But I would like to read URL which is on browser (with alias):
http://xyz/en/feedback?ID=123456&Email=abc#hotmail.com
feedback is a Joomla alias. I'm redirecting that URL with variable from another PHP script.
If I try:
$_SERVER['HTTP_REFERER']; // not working in IE.
$_SERVER['QUERY_STRING'] //return com_xyz&format=raw&tmpl=component&lang=en
JRequest::getVar('Email'); // return NULL but works if try getVar('lang');
Is there any other solution or better way?
Try using the following. Might be what you're looking for:
$url = JUri::current();
echo $url;
Using Javascript, you could use this:
document.location.href

PHP: Adding security with http referer [duplicate]

This question already has answers here:
Determining Referer in PHP
(5 answers)
Closed 8 years ago.
Im Kinda A Noob With PHP,
I want to keep my page accessible only from a link
etc. I only want to allow people who clicked a link to my page from example.com
and others like from google.com to redirect to another page on my site etc. a error message
How Could I Do This?
if(isset($_SERVER['HTTP_REFERER']))
$referer_host = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
else
$referer_host = '';
if($referer_host != 'example.com')
{
header('Location: http://example.com/error');
exit;
}
People not sending (correct) referers for various reasons will be entirely excluded from your page.
Of course bookmarking your site etc. will also not work.
As headers can be faked by the client at will, I would not call this a "security" feature.

How can i create pages in PHP without create physical pages? [duplicate]

This question already has answers here:
Reference: mod_rewrite, URL rewriting and "pretty links" explained
(5 answers)
Closed 8 years ago.
How can i create pages in php, but don't create physical pages. The page don't exist in disk.
I don't want create a physical page in disk for each page ... Image with 3 thousand pages ...
Well, that's really general question. I assume that you're a beginner. Sorry if you're not but that what it seems to me like.
There are like 100 ways of creating website using PHP. PHP is there so you don't have to, like you said, create separate file for each page. If you did, it would be just a simple templating (you create HTML layout and put little PHP scripts inside so you can generate content dynamically).
I believe this isn't right place to ask. Instead you should look up some tutorials, get the picture of how PHP works in general and than maybe try something out and THAN ask again.
W3Schools.com is a great website to start with. Great tutorials, learned a lot there :-)
I think you want to avoid creating physical files on the disk.
The simplest thing comes in my mind, as you can use $_GET.
Take a look at this code.
index.php :
<?php
// Get the "page" information from HTTP GET.
$page = (isset($_GET["page"]) ? $_GET["page"]; : "default";
// Display content according to the page information from GET.
switch($page) {
case "foo_page":
echo "this is foo page.";
break;
case "bar_page":
echo "this is bar page.";
break;
default:
echo "this is the default page.";
break;
}
?>
Now your index.php will respond
this is the default page.
But when you go to index.php?page=foo_page, you'll see that it outputs
this is foo page.
Then you could rewrite it by a .htaccess file.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_]+)/?$ index.php?page=$1 [NC,L]
Which would make your url's cleaner, like :
domain.com/foo_page/
domain.com/bar_page/
As i've told, this is the first thing i thought of when i read the question.
Hope this helps, or at least gives an idea.
I think you have lot's of images to display in page by page.
Like page1|page2|etccc
if so
First you should plan how it works. In this case , ill do
initialize a variable noofimage=30
another variable start-from=startfrom * finish.
initially start-from=0; // because it start from 0 to noofimage you specified to show in single page.Place this line below nofimage;
do query in php to start from "start-from" to "noofimage"
get total images and divide it by noofimage.
if you have 3000 images then 3000/30=100
so now you have 100 pages.
now using while loop create a page link
int i=1 // start from page 1
while(page<=100)
{
<a href="yoursite.com/images.php?pageno=$i>
i++ // increment page
}
now in your sitebelow
page1|page2|page3|.....
wikk created ....
if some one clicks a page.
at server ,
php files first
$start-from=$_GET['pageno']
$finish= $start-from * $ noofimage;
$start-from=($start-from -1)* $ noofimage
// we are using startfrom- 1 because , in next page images should start from 30 to 60. so we get a page multiple it with noofimages per page for $finish and for start we just decrement 1 then multiple it with noofimage.
now , use php mysql query to (start from , end);
// I just shared a idea how to do php for this page.
try it , or ask if you have any problems.

Block access to files [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Preventing direct access to php files
I want to block direct access to certain files so that they can only be used by PHP via include or require.
What I'm doing on the first line of each file is this:
<?php
if(stristr(__FILE__,substr($_SERVER['PHP_SELF'],strrpos($_SERVER['PHP_SELF'],"/")+1))){
header("HTTP/1.0 404 Not Found");
exit;
}
?>
Is that a good way of doing this, or is there a better way?
Thank you.
One better option would be to move the files out of the website public area into a private directory, then use set_include_path (or equivalent server config) to allow include and requre to find them.

Categories