I have use this to generate this code:
<?php
require_once('mobile_device_detect.php');
mobile_device_detect(true,false,true,true, true,true,true,'http://m.mydomain.com',false);
?>
But the only directions are to "copy and paste this code". Um.. copy and paste where? Do I need to create a new php file? Is this index.php? What if I already have an index.html file?
EDIT: I understand that I put mobile_device_detect.php in the root of mydomain.com. My question is where to put the above php code.
Copy and paste this at the beginning of your PHP based pages that you want to detect the visitors for their device. If your server parses HTML files as PHP which I doubt then add this in your HTML files as well. If you're just building the website then yes you need this in files which are parsed by the PHP engine for example: ".php".
If you paste this in page that is HTML and not parsed by the server you'll see this same code as output which will do nothing. In order to have it working you need it in PHP files.
If your script is well written and well structured you may need to include it in only one place. It all depends how your website is structured.
------ UPDATE ------
Why you shouldn't be using this class? It have a special license which is not absolutely free.
Instead you can use this simple class: https://github.com/serbanghita/Mobile-Detect
Download Mobile_Detect.php
Include the file at the top in your PHP page where you want the device to be checked:
// Include the mobile device detect class
include 'Mobile_Detect.php';
// Init the class
$detect = new Mobile_Detect();
// And here is the magic - checking if the user comes with a mobile device
if ($detect->isMobile()) {
// Detects any mobile device.
// Redirecting
header("Location: http://your_redirected_url.com"); exit;
}
Creating rewrite rules for using html extension.
If you still want to use '.html' as extension just create rewrite rule that will rewrite your .php as .html. Or otherwise said create your_page_name.php and add the PHP code there. Create .htaccess file in the same DIR and add the following lines:
RewriteEngine On
RewriteBase /
RewriteRule ^your_page_name.html/?$ your_page_name.php [L]
Save, close! Now you should be able to use your php page with .html extension. To access your page now just type: http://yourdomain.com/your_page_name.html
Simple as that!
Suggestion: If I was you I'd add the rewrite rules in the web server's config file. It will be faster and more secure. But that's another lesson. If you decide to use this method just search the Stack.
Copy and paste the code anywhere you want. Just make sure the function is defined on any page that needs it.
You should either buy the script mobile_device_detect.php from the site or use a free method called pay with a tweet option.. Go to the download page and you will see them there..
ok, in case this helps someone, here are the details of what's working for me:
Create an index.php file with just this:
<?php
require_once('mobile_device_detect.php');
$mobile = mobile_device_detect();
// redirect all mobiles to mobile site and all other browsers to desktop site
if($mobile==true){
header('Location:http://m.yourdomain.com/');
}else{
header('Location:http://yourdomain.com/index.html');
}
exit;
?>
Drop the mobile_device_detect.php file in the root of your site.
Then, add this line to your .htaccess file:
DirectoryIndex index.php index.html
Related
I'm looking for a little help from someone who knows PHP,
in short, it looks like this:
I once made a website for a customer with products on the Jomla CMS system, and the product was located, for example, at:
www.mysite.pl/index.php/productall/product1
www.mysite.pl/index.php/productall/product2
e.t.c...
currently his site has been changed and is in the classic html5 / css / js without any CMS, but in the same old directory
and the product is on:
www.mysite.pl/product1.html
The client created QR codes for himself, but to the old page and printed on each product
and now I would like to create an index.php file, which will redirect the traffic depending on the call in the browser, because now as the QR code is being scanned it jumped out because there is no such adreas as before.
For now, I created an index.php file that redirects to
www.mysite.pl/allproduct.hml
where is the list of all products but I would prefer it to be as before
that is, each QRcode was redirected to the chosen product.
Can you do it somehow? PLEASE HELP
now i have this code in index.php
Instead of messing with PHP, You could try adding redirect commands within the htaccess file? (if you can still use one?) The ".htaccess" file should be located in the site root.
ie: /public_html/.htaccess
within this file you can create redirects from the old address to the new one.
Redirect 301 /oldProductLink1.html http://www.example.com/NewProductPage1.html
Redirect 301 /oldProductLink2.html http://www.example.com/NewProductPage2.html
:)
There's no easy solution you must create a .htacess file and place on your root folder then redirect all the old urls to new url for each product
Redirect 301 /index.php/productall/product1 www.mysite.pl/product1.html
Redirect 301 /index.php/productall/product1 www.mysite.pl/product2.html
Redirect 301 /index.php/productall/product1 www.mysite.pl/product3.html
Redirect 301 /index.php/productall/product1 www.mysite.pl/product4.html
If the question is only about PHP and there is no way to use web server URL rewriting or redirect, then you should create files like :
/index.php/productall/product1
/index.php/productall/product2
Where index.php would be a directory. Each file should contain something like :
header('location: /product1.html');
exit();
Where the product number should be set according to file name.
i still have this:
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
I removed this my test index.php file and created .htaccess in the root directory with yours codes
So i'm wondering how you professionals go about securing your website by not allowing a user to manually input a url to a certain folder such as website.com/images/ and view a directory listing. The method i've been using is placing an index.php file and using this code, which just brings the user back to main homepage. But i feel like there is probably an easier way to do this. Possibly Server Side?
<?php
ob_start();
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: ../index.php');
exit;
?>
Use htaccess to prevent direct access
create .htaccess file add this Options -Indexes line . add it in your project root path
These links may help you
1. deny directory listing with htaccess
2. http://viralpatel.net/blogs/htaccess-directory-listing-enable-disable-allow-deny-prevent-htaccess-directory-listing/
As a precaution I am wanting to use PHP to create an easily reusable/modifiable means to redirect users to a specified URL.
I have the usual php header redirect:
<?php
header( 'Location: http://www.stackoverflow.com/' ) ;
?>
What I'd prefer to do however, as this file will be placed across a lot of directories, to make life easier, and have the url extracted from an external file, e.g.
http://www.stackoverflow.com/url.xml
This would of course contain the URL of the website in question, unless there is another way to capture the domain itself automatically? This I'm not sure.
Could anyone be kind enough to show how this would be done or provide the best approach?
Thank you.
I was thinking in the same approach as #adam , i don't recommend you to extract urls from a file because it can be read from an attacker. It's better to include them in a php file as variables, an array or any other data structure.
Store the urls once in a file called config.inc.php:
<?php
define('USER_PATH', '/redirect/user/path');
define('ADMIN_PATH', '/redirect/admin/path');
?>
Then in your php file:
<?php
include 'config.inc.php';
header( "Location: http://{$_SERVER['HTTP_HOST']} . USER_PATH ) ; //or whatever variable you want to use to redirect.
?>
If you want to redirect some folders due to security issues you can do it with an Apache's htaccess.
Just put this at root folder then add the last line as many as you want for each redirection.
Each folder mentioned here can only be accessed by scripts running on the server.
Using this every access request using HTTP gets redirected. So this only works for directories containing scripts to be included. It doesn't work for i.e. image directories whose index shouldn't be shown, but the images should stay accessible.
RewriteEngine on
RewriteBase /
RewriteRule ^includes/(.*) http://www.stackoverflow.com/ [R=301,L]
RewriteRule ^includes/(.*) http://www.stackoverflow.com/ [R=301,L]
If you aren't familiar with this you can use http://www.htaccessredirect.net/
If the file containing the url is on a domain you don't control, there are security risks.
Instead, store the url once in a file called config.inc.php:
<?php
define('REDIRECT_URL', '/redirect/path');
?>
Then include it in your project:
<?php
include 'config.inc.php';
echo REDIRECT_URL;
// /redirect/path
?>
I am relatively new to php. There is a very basic thing that has been troubling me. I understand that php is used to make web sites dynamic. I also understand that php is one of the many server side scripting languages that can be used to make dynamic web sites.
However, what I do not understand is that when do i need to use an index.php page. Say for example if i have just a simple login page on my index page, it could very well just be a simple html page as well. Right? Then why would i want to make it index.php instead of index.html?
An example of a sample situation would be great.
You will have to choose the PHP extension (.php) when you want php code to be executed in the file. PHP code is code between the opening <?php or <? and the closing ?> tags.
When no PHP code should be executed you can use the .html extension.
Usually when using the .php extension you are telling the web server, that it should use a php interpreter to process the file before it will be delivered to the browser. The php interpreter will then replace all content between the <?php and ?> by the output of the PHP code. Just as if you wrote it manually. The processed file will then be delivered to the browser.
However, using the .php extension to tell the web server to process php code is configurable. If you want you can use other file extensions too.
There is another thing that should be pointed out. When you only type the url path (without a filename) like :
http://www.myserver.com/
there is an order of extensions (filenames) which the webserver (apache) searches for an index document. For example an apache config may contain a section like:
<IfModule mod_dir.c>
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
Meaning that the index document is searched in the order above. This means if you place an index.html and a index.php in the same folder - and having the configuration above - always the index.html would be delivered by the server.
It will not hurt a website if you serve every page as .php. Apache is very fast to serve any php, let alone one which contains only static html.
As a beginner you may find php will benefit you, by allowing you to creating simple templates. Site header and footer includes for instance could be written in one file, then included in all the other pages.
Its not a big deal whether you use index.php or index.html. You ca use anyone of either. Only thing is you need PHP(or any other server side scripting language) to make your site dynamic.
Like you have a login page,you can surely make it as inde.html but your logics would either have to be in a different file or embedded in HTMl.
You can use which-ever you prefer:
If you prefer keeping forms and basic pages that don't use data in HTML,
and keep pages that use php in php format that is fine.
But one method I and I assume most others use is just make all of your pages php files. This is because you can include a html document in the php file and display it just the same. But you cannot do php queries from a html file so it is easy to just always use php just incase you want to add some php scripts to it.
Index.php:
<?php
$var = 5;
?>
<html>
<head>
<title>My Page</title>
</head>
<body>
<h2>Your variable was <?php echo $var; ?></h2>
</body>
</html>
By default, the apache server needs a file with .php extension to parse the PHP code inside it. Though, if you wish, you may configure your server, by adding a single line to the configuration file, to use files with any extension with PHP code inside it. You can edit the apache by yourself to support php also in .HTML extension.
In simple terms, you can easily access index.html file and get the data beneath it.But index.php is difficult to access. For your simple application index.html will do the trick. If you are planning for some big and secure application go for index.php
It is beacuse sometimes you may have some logic written on index.php. Like you may check if user is logged in or not and then redirect user to some specific page. Also you may do device based redirection as in case of mobile devices.
You can always choose to create index.html but you do not know when youy may need to have some logic there.
For example, in my index.php or another main page I mainly use php because I use variables w/ them in the rest of my site:
<?php
$sitepath="http://www.example.com/public";
$author="your name here";
?>
Because I <?php echo $sitepath ?> every time I link an image in my website so it doesn't break or something. Since I'm reusing the name all the time, I use .php to be able to have that service, because if I use the name, I can change it globally.
I think that simple pages like 404.html, aboutus.html, or ViewOneBlogPost.html could be an HTML page, you might not need any functionality/variables for that.
To check current settings for file extension priority in apache2 with linux
/etc/apache2/mods-enabled/dir.conf
Well you can use HTML if you have a simple few pages where the code doesn't repeat itself a lot. But trust me, if you have a whole website in HTML with more than a thousand lines per file, you will want to use PHP. Why? Because of require and include. These will clean up your code when you need to use layouts, so you don't need to keep copying the hole code of your layout header, footer etc... It can be really difficult to maintain when you need to insert content and there's a lot of code in the way.
Here's some documentation about it
https://www.php.net/manual/en/function.include.php
I have designed a website, and within it I have a range of PHP scripts which interact with my system. For example, if a user uploads an image, this is processed by the script
image.php
and if a user logs in this is processed by the script
login.php
All these scripts are stored in the folder called: scripts
How do I ensure someone cannot access these pages, however still ensure they can be used by the system? I want to ensure the PHP pages will accept post values, get values and can redirect to other pages, but not be directly accessed via the address bar or downloaded?
I attempted to block access using .htaccess using deny from all and Limit GET, POST but this prevented the system from working as I could not access those files at all.
Blocking files with htaccess makes the files inaccessible to the requestor, e.g. the visitor of the page. So you need a proxy file to pass the visitor's request to the files. For that, have a look at the MVC pattern and the Front Controller pattern.
Basically, what you will want to do is route all requests to a single point of entry, e.g. index.php and decide from there, which action(your scripts) is called to process the request. Then you could place your scripts and templates outside the publicly accessible folder or, if that is impossible (on some shared hosts), protect the folders with htaccess like you already did (DENY FROM ALL) then.
To use the upload script you'd have a URL like http://example.com/index.php?action=upload.
A supersimple FrontController is as easy as
$scriptPath = 'path/to/your/scripts/directory/';
$defaultAction = 'action404.php';
$requestedAction = $_GET['action']; // you might want to sanitize this
switch($action) {
case 'upload':
$actionScript = 'image.php';
break;
case 'login':
$actionScript = 'login.php';
break;
default:
$actionScript = $defaultAction;
}
include $scriptPath . $actionScript;
exit;
Your actionScript would then do everything you need to do with the request, including redirection, db access, authentication, uploading stuff, rendering templates, etc - whatever you deem necessary. The default action in the example above could look like this:
<?php // action404.php
header('HTTP/1.1 404 File Not Found');
fpassthru('path/to/template/directory/error404.html');
There is numerous implementations of the FrontController pattern in PHP. Some simple, some complex. The CodeIgniter framework uses a lightweight MVC/FrontController implementation that might not be too overwhelming if this is new to to you.
Like Atli above suggested, you could use mod_rewrite to force all requests to index.php and you could also use it to pretty up your URLs. This is common practice with MVC frameworks and has been covered extensively here and elsewhere.
You can't really prevent direct requests to the files, and still have them remain accessible to other requests. The best you can do is mask their location, and control how they are accessed.
One way you could go is to create a PHP "switch" script, which would include the scripts for you, rather than have Apache request them directly.
For example, if you had your scripts/image.php rule target switch.php?file=image.php instead, somewhat like:
RewriteRule ([^\.]+\.(jpe?g|png|gif)$ switch.php?file=image.php&rw=1&meta=$1 [L,QSA]
You could add deny from all to the scripts/.htaccess file and do this in your switch.php file.
<?php
/** File: switch.php **/
$allowed_files = array(
'login.php',
'image.php'
);
$script_dir = 'scripts/';
if(isset($_POST['rw']) && in_array($_REQUEST['file'], $allowed_files)) {
include $script_dir . $allowed_files[$_REQUEST['file']];
}
else {
header('HTTP/1.1 404 File Not Found');
include 'error404.html'; // Or something to that effect.
}
?>
The $_POST['rw'] there is a weak check, to see if the rule came from a RewriteRule, meant to prevent direct requests to the file. Pretty easy to bypass if you know it is there, but effective against random requests by bots and such.
This way, direct requests to either scripts/image.php and switch.php?file=image.php would fail, but requests to any image file would trigger the scripts/image.php script.
You can set deny from all on .htaccess and include these files from some accessible directory
I want to ensure the PHP pages will accept post values, get values and can redirect to other pages, but not be directly accessed via the address bar or downloaded?
As long as Apache is configured to associate all .php files with the PHP application, no one can download the PHP content itself. So, if someone browsed to "mysite.com/image.php", PHP will run. The user will NOT see your PHP content.
This should already by done in your httpd.conf file as :
AddType application/x-httpd-php .php .phtml
Now, image.php will be expecting certain post parameters. Short of implementing an MVC architecture as Atli suggested above, you could gracefully and securely deal with any missing parameters if they aren't provided. Then, users can get to the page directly but not do anything with it.
A lot of applications just put files like your scripts not in the public (like /public_html/ or /www/) folder but in the same root folder as your public folder.
so not
root/public_html/ and
root/public_html/scripts/
but
root/public_html/ and
root/scripts/
Anything in a folder above the public folder can't be accessed by visitors, but by specifying in for example /public_html/index.php the file '../scripts/yourscript.php' PHP can access these files and visitors can't. (the folder ../ means "go up one step in the folder hierarchy")