Package: django-php-bridge
=========================================================django-php-bridge - Authentication betwen Django and PHP=========================================================*******Purpose*******`Django-PHP-Bridge`_ is a Django authentication backend that allows your Djangoproject to seemlessly pass users to and from a PHP application. This allowsyou to build an application with both PHP and Django components while keeping asolid user experience.Whether you're porting from PHP to Django, from Django to PHP, integrating twodistinct applications or building a hybrid app, `Django-PHP-Bridge`_ aims tohelp make the distinction irrelevant to your users.****************General Approach****************There are several different ways to approach this problem, mostly revolvingaround which side's default behavior you use and which side needs morecustomization. In general, we've taken (and this documentation assumes) thatDjango's default behavior should be used where possible. However, it iscompletely possible to use the provided session backend as part of a morePHP-centric approach.Django Defaults Used====================* ``django.contrib.auth.models.User`` is used to store Users, with the standarddjango Profile extension recommended for additional fields.* The database is used as the session store, but this could easily becustomized.PHP Defaults Used=================* ``django-php-bridge.backends.db`` uses PHP's native serialization format tostore session data.*****Usage*****This usage guide assumes a few things about your setup.1. You're using Django's ``django.contrib.auth.backends.ModelBackend`` as yourauthentication backend and you want to use ``django.contrib.auth.models.User``for storing basic user information.2. Your PHP and Django projects share a database. That's how the sessioncoordination is accomplished.3. If you had a custom schema to store your user and profile information,you've already converted it to Django's schema.Usage: Django Side==================On the django side of your project, installation is fairly simple.Install `Django-PHP-Bridge`_::$ pip install django-php-bridgeConfigure your Django project to use the PHP-compatible session backend byadding the following to your ``settings.py`` ::SESSION_ENGINE = 'django_php_bridge.backends.db'SESSION_COOKIE_NAME = 'PHPSESSID'Let your Django project know that you'll be using the PHP side of your projectto do actual logins. You do this by setting the ``LOGIN_URL`` setting in``settings.py`` to point to the PHP-served URL that will be handling yourlogin. eg.::LOGIN_URL = '/'Usage: PHP Side===============Installation and setup on the PHP side is complicated by the fact that PHPapplications are all generally very different. A helper/guide for usingDjango-PHP-Bridge with common PHP frameworks like `CakePHP`_ and `Symfony`_would be easier to write (and would be an appreciated contribution).In general, the steps involved are:Create and Use a Compatible Session Table-----------------------------------------The session table you use needs to be compatible with the schema that Djangoexpects. The exact SQL to create the table will vary, but the Django Docs onthe `sql command`_ show us an easy way to obtain the SQL from your djangoproject by running::$ django-admin.py sql sessionsIf you're using MySQL, you can use ``contrib/mysql/django_session_table.sql``Alternatively, you can use Django's syncdb to create the table::$ manage.py syncdb.. _`sql command`: http://docs.djangoproject.com/en/dev/ref/django-admin/#sql-appname-appnamePlace the Appropriate Session-Handler on Every Page---------------------------------------------------PHP allows for `custom session handlers`_ to be defined, which allows us touse the django_session table we created above. The session handler you use willneed to be aware of the django_session table's schema and you'll need toregister this session handler on every page *before* calling ``session_start();``.An example session handler class is provided in``contrib/php/djangoSession.class.php``... _`custom session handlers`: http://php.net/manual/en/session.customhandler.phpCreate and Use a Compatible User Table--------------------------------------In order for any reasonable level of integration, most projects will need toknow who users are on both the PHP and Django side. Because most generalPHP projects vary greatly in how they store their user information, if comingfrom an existing PHP project, this will probably require some custom work toconvert user data. Django applications generally use a User model plus aProfile model to store user data. See the `Django Auth Documentation`_ fordetails.Included is an example of a PHP class that relies on the same schema as``django.contrib.auth.models.User`` as an example and starting point. It knowsa little bit about how Django stores passwords and what fields are necessary,but it will certainly need tweaking to work with your existing PHPproject. The file is located at ``contrib/php/user.class.php``.Suggestions and contributions to make this part of the integration processeasier are welcome... _`Django Auth Documentation`: http://docs.djangoproject.com/en/1.3/topics/auth/Configure URLs Handled by PHP vs Django---------------------------------------The final piece of integration will be to tell your web server how to determineif a given request should be resolved by the Django side or by the PHP side.This means changing your configuration so that for example, everything at``/account`` is served by Django and everything at ``/blog`` is served by PHP.If you're using different domains or subdomains to separate the side of your app,then you can ignore this step.Generally, to keep this part sane, you'll want to file good URL practices andseparate which side of your project handles particular tasks and domain objects.Django's application-centric ``urls.py`` configuration makes this easy.Particular attention should be paid with regards to which side of your projectshould handle logging in and logging out. It's generally simpler if eitheronly Django or only PHP handles both logging in and logging out users andprobably simpler if that same side handles registration and account editing.In the case of `Apache2`_ running `mod_wsgi`_ for Django and mod_php (orsimilar) for PHP, the separation can be accomplished inside a VirtualHost file.An example vhost file is provided at ``contrib/apache2/vhost_conf``.*******History*******This authentication backend was extracted from code used in production bya saas policy management start called `PolicyStat`_ during their multi-yearconversion from a PHP application to a `Django`_ application. You can reada bit about their `PHP to Django Conversion`_.`PolicyStat`_ has sense converted to 100% Django and is no longer using thisapproach in production, but the hope is that someone who is will be interestedin taking an active role in this project.************Contributing************All development on Django-PHP-Bridge happens at Github: http://github.com/winhamwr/django-php-bridgeYou are highly encourage to contribute to the improvement of Django-PHP-Bridge.We would especially love contributions along the lines of how to integrate withspecific PHP frameworks.***********Bug tracker***********If you have any suggestions, bug reports or questions please report themto our issue tracker at http://github.com/winhamwr/django-php-bridge/issues/Also feel free to tweet @weswinham on twitter... For full documenation, you can build the `sphinx`_ documentation yourself or.. vist the `online Django-PHP-Bridge documentation`_.. _`Django-PHP-Bridge`: http://github.com/winhamwr/django-php-bridge/.. _`Policystat`: http://policystat.com.. _`Django`: http://www.djangoproject.com/.. _`CakePHP`: http://cakephp.org/.. _`Symfony`: http://www.symfony-project.org/.. _`Apache2`: http://httpd.apache.org/.. _`mod_wsgi`: http://www.modwsgi.org/.. _`PHP to Django Conversion`: http://devblog.policystat.com/php-to-django-changing-the-engine-while-the-c.. _`sphinx`: http://sphinx.pocoo.org/.. _`online Django-PHP-Bridge documentation`: http://readthedocs.org/projects/django-php-bridge/