Website Authentication using PHP in HTML for CGI Website? Alternatives? [duplicate] - php

This question already has answers here:
How do I add PHP code/file to HTML(.html) files?
(12 answers)
Closed 8 years ago.
I am creating a website for my organization that also has some web applications that run server side via CGI using Python. I am wanting a little bit more of a intuitive front end framework. I am also more familiar with Scripting languages then web languages. I have chosen Bootstrap as my frame work for it's user base and bound of documentation.
Now I am understanding the website authentication in regards to PHP and mySQL. But I have a question that I have not been able to find the answer to.
How do I set up website authentication via PHP and mySQL but not have every page end in .php?
For example is it possible to somehow insert this CheckLogin code at the beginning of but inside the html code of a webpage that is .html instead of .php:
<?PHP
require_once("./include/membersite_config.php");
if(!$fgmembersite->CheckLogin())
{
$fgmembersite->RedirectToURL("login.php");
exit;
}
?>
The CheckLogin code:
function CheckLogin()
{
session_start();
$sessionvar = $this->GetLoginSessionVar();
if(empty($_SESSION[$sessionvar]))
{
return false;
}
return true;
I am referencing this tutorial:http://www.html-form-guide.com/php-form/php-login-form.html
Update: I am using Apache.
I am looking for some guidance besides just the name of the technology, something with a example or tutorial as again I am very new to webside programming and I would benefit most from some real world type of examples.
EDIT: I am also looking for alternatives that would be best suited for CGI type of websites. Anything in the realm of Apache and website authentication based around integration with CGI.
This question was marked as duplicate but I disagree as it has more caveats do to the CGI nature instead of how to simply insert PHP into HTML. The most common solution found when researching "Web site Authentication" is PHP. Hence why asking how to make a CGI website turn by turning PHP pages into HTML pages for the sake of simplicity of using CGI. If there are easier ways to do this by all means please share.

There's a few alternatives. One is to have Apache parse .html files as .php files by adding this to your .htaccess:
AddType application/x-httpd-php .html
Then you could simply add your script to the top of your .html files and it would work. However a cleaner solution would be to use mod_rewrite. For example, you could route www.example.com/page to www.example.com/page.php with
# .htaccess
# Enable Rewriting
RewriteEngine on
# Rewrite URLs
RewriteRule ^/(.+)/?$ $1.php
Or you could have one file as the router:
# .htaccess
# Enable Rewriting
RewriteEngine on
# Rewrite URLs
RewriteRule ^/(.+)/?$ index.php?page=$1
This version would pass whatever comes after www.example.com/ as a $_GET['page'] variable to your index.php file.
Note: I didn't test these, but they should work.
Here's a pretty nice tutorial for some more info: http://code.tutsplus.com/tutorials/an-in-depth-guide-to-mod_rewrite-for-apache--net-6708

as Halcyon wrote, it is not possible to combine your Authentification with .php and .html, because in .html you can't extract the session vars.
But as he wrote, you can use mod_rewrite to display the page not as ".php" but as ".html".
Regards
Mike

Related

How to configure URLs using PHP [duplicate]

This question already has answers here:
How to Implement URL Routing in PHP
(2 answers)
Closed 9 years ago.
Is there a way to make this URL:
http://readme.com/read?blog=10092
look like this:
http://readme.com/read/blog/10092
Using PHP?
It is not possible via PHP itself. Even the above example (read?blog) is not a php-only solution.
PHP is a parsed file. The webserver parses a .php script and displays it to the viewer. So you have to configure your server (Apach for example) to use the correct php file for the request. The most common solution is mod_rewrite (http://httpd.apache.org/docs/current/mod/mod_rewrite.html an lots of tutorials). You will have to edit yout .httaccess file and create configuration options.
One technique is by rewriting the url using .htaccess. Create a .htaccess (There's a dot in front of the file name!) file inside of your root folder and place the following code inside:
Options +FollowSymLinks
RewriteEngine on
RewriteRule /blog/(.*)/ read?blog=$1
Now you can go to the same page using the url :
http://readme.com/read/blog/10092
It looks like you need a PHP framework, like CakePHP or CodeIgniter. One of their main feature is the routine mechanism. However, you must first understand some Object-Oriented Programming in PHP.
Its more complicated with plain php, you have to use htaccess too. See this example:
RewriteEngine on
RewriteRule ^/read/blog/([0-9]+)$ /read?blog=$1
Url rewriting is powered by .htaccess and inside it, regular expressions. If you want to know more about regular expressions, see wikipedia: https://en.wikipedia.org/wiki/Regular_expression

Clean URL with php and apache [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Rewrite all queries to not need the .php extension using a mod_rewrite RewriteRule
I am learning how to create a website, and would like some help.
I have this path in the wamp folder: C:\wamp\www\Personal_site\Root_fold\index.php (the main file starts here).
I want to have cleanurls for this file: C:\wamp\www\Personal_site\Root_fold\Tutorials\C_sharp\C_loginapp.php, and I want it to show up as C:\wamp\www\Personal_site\Root_fold\Tutorials\C_sharp\C_loginapp - the same file, but without the .php on the end.
I also want this to happen to every other file that I have in my website - even the index.php - without repeating the rewritecond for each and every file. How do I do that?
By the way, I am running this locally on my computer - no hosting service or anything like that - and also I am creating the .htaccess file by opening it in notepad and saving the file with the name ".htacess", which means it forces the file to save with the .htacess extension.
Clean URLs go beyond just hiding the .php extension. It also needs to incorporate query parameters, so that instead of /article?title=foobar it looks like /article/foobar.
This problem was solved over and over and over again. Please don't participate in the PHP community's pervasive Not invented here syndrome and use one of the existing solutions available. It will make you a better programmer in general, because today's programming is about artfully combining existing components with your unique domain logic instead of writing everything from scratch.
UPDATE
Since you are just starting, it might be hard for you to start using a full-blown framework like Symfony2 — but I suggest to use it when you'll be more comfortable with PHP.
For now, the Silex micro-framework might be a good start for you. It's very easy to start with. As a simple example, here is your index.php file:
require_once __DIR__.'/silex.phar';
$app = new Silex\Application();
$app->get('/hello/{name}', function($name) use($app) {
return 'Hello '.$app->escape($name);
});
$app->run();
And this is your .htaccess:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
This is a complete app written in Silex. When you go to <yourhost>/hello/JackyBoi it will respond with Hello JackyBoi.
You can do more advanced stuff with Silex — details are in the docs. When you outgrow it, you can naturally graduate to its bigger brother Symfony2.
Add this to your .htacceess:
Options +MultiViews
Remove all the rewriterules. Then you can use URLs (not filenames!!) without the .php extension.

Replace articles.php?id=123 with /articles/123-title-of-article

I am building a php+mysql site which will have numerous articles. I am pretty ok with html php jquery etc. I need to now what are the steps I need to take in order not have http://www.mysite.com/articles.php?id=123 but to have http://www.mysite.com/articles/123-title-of-article?
Thanks
Well, you need, for instance, to store the token for each article (to optimize), like your example "123-title-of-article". Next step is to use .htaccess and mod_rewrite from Apache (if is your case).
If you will do it only on this articles "page folder" (articles/), I suggest to you make this folder valid and make the .htaccess inside that and redirect to articles.php?id={POST_ID_FOUND_BY_TOKEN}.
Take a look too on $_SERVER["REQUEST_*"] variables from PHP.
See: this page, on end of article have some examples.
The usual way to do this is by using mod_rewrite.
You create a .htaccess file which, behind the scene, redirects the latter request to the former.
You can read about it here: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
You'll need something called mod_rewrite, which is an apache module.
The configuration looks like this (stolen from a client's drupal install):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule articles/(.*)$ articles.php?id=$1 [L,QSA]
</IfModule>
I haven't tested this, but it should work.

How can I put Pyramid in front of a PHP website using the same webserver?

The scenario is: I current have an old website that runs on PHP. Over time, that code has become hacked up and messy. It's due for a rewrite. However, I don't have time to do that rewrite yet. But I'd like to plan for it in the future.
What I need to do now is add a 'welcome' type page to the website. I'd like to code that in Python using the Pyramid framework.
The old sites URL structure is this:
http://website.com/XXXXXX
Where the X is the short URL id.
What I was thinking of doing was using Apaches LocationMatch config to catch the short URL. Something like:
<LocationMatch "/^([a-zA-Z0-9]{6})$">
This would then redirect the request to the PHP portion of the website. Everything else would be caught by Pyramid.
I'm not sure how to action this. Does Apache have an else type clause for LocationMatch? How would I tell it to serve the PHP files for /XXXXXX matches and send everything else to Pyramid?
Server Notes:
Apache2 + PHP (Debian package)
mod_wsgi 3.3
Python2.7
I am not sure about Apache configuration, but you could use wphp, a wsgi middleware for serving php.
http://pythonpaste.org/wphp/
Use recipes for using AddHandler described in:
http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines#The_Apache_Alias_Directive
Apply the AddHandler and the rewrite rule to your DocumentRoot directory. Then add the SCRIPT_NAME fixup to your WSGI script file.

Domain/URL Masking

I have a website that passes some GET variables to different pages in PHP. My issue is that now I have a url with variables i.e. index.php?category=categoryname and that's not very memorable.
Is there any way I can change the URL to something like /categoryname instead without duplicating the page and storing in folders? But also allow users to type in /categoryname and be redirected to the correct page?
.htaccess Apache mod_rewrite, almost every professional dynamic website uses this method (like stackoverflow).
The method is fully explained in this article far better then I could ever explain it in this answer box.
You should look into writing some apache Mod_Rewrite rules in a .htaccess file.
The solution is discussed here:
this is done by the rewrite module of apache and this handles regular
expressions. You have to put a rule
like this in your .htaccess file on
the root of your website:
RewriteRule ^cat/([0-9]+)$
/index.php?category=$1
^ means the start of the url after
www.example.com/ $ means the end of
the page.
www.example.com/cat/123
will be converted by the server to:
www.example.com/index.php?category=123
In PHP you use the normal $_GET['id']
variable. The rewrite module must be
enabled by apache... This is mostly
used to make the url format
independent of the serverside
scripting language so the .php in the
url is not logical. Thats why i
changed it to product/ . The .htaccess
starts with
RewriteEngine On Options
+FollowSymLinks RewriteBase / Here all the rewrite rules.. ...

Categories