How to intergrate Django with PHP

3,105 views
Skip to first unread message

rajivb

unread,
Feb 27, 2008, 9:48:10 AM2/27/08
to Django users
Hey guys,

I m new to web development..By the way i created one application in
PHP. now i want that for login part/admin part i use Django.. So how
can i intergrate these two(i mean existing php code with Django)??

Please help .....

Charles

unread,
Feb 27, 2008, 10:06:15 AM2/27/08
to Django users
Hi Rajivb,

That's a really odd idea. Why on earth do you want to do such a thing?

You can use Django to build your whole application (admin interface +
front-end interface).
There is no point to mix php code and python code in that case.
Either you choose a php web framework (such as Symfony) or a python
web framework (such as Django).

If you have already started to build something using php then I would
advise you to go for a php framework. Symfony (http://www.symfony-
project.org/) is a MVC web framework that works the same way as Django
and provides, as Django, an auto-generated admin (what you call 'login
part/admin part'). You should give a try...

Personally, I prefer Django by far.

Charles.

rajivb

unread,
Feb 27, 2008, 10:23:38 AM2/27/08
to Django users
Hey Charles,

Really thanx for ur time and great suggestion..

Actually i got stuck in a problem..I am having a complete application
written in PHP. You can assume that i need to integrate it with a lot
of applications which supports Django interface. Due to time
contraints it not possible for me to rewrite the whole code...So i
thought of an solution like if for log-in purpose somehow i'll use
django so that i'll be able to integrate it with other apps also..so
thats why i asked is there any way so that i can have login thing in
Django and other things in PHP..

If there is a way so that i cn integrate Django with PHP with less
number of cons so may be i cn thnk of it ;-)
Thanks again for your time ;)

Rajivb
> > Please help .....- Hide quoted text -
>
> - Show quoted text -

kevinski

unread,
Feb 27, 2008, 10:35:35 AM2/27/08
to Django users
> thats why i asked is there any way so that i can have login thing in
> Django and other things in PHP..
>
> If there is a way so that i cn integrate Django with PHP with less
> number of cons so may be i cn thnk of it ;-)

Did you ever consider embedding PHP pages into your Django app using
iframes? I borrowed a couple PHP apps that I would have had a lot of
trouble writing in Python due to complex API usage, so I embedded them
and they work great and nobody can tell they are not part of the app.

Just a suggestion.

Kevin

rajiv bammi

unread,
Feb 27, 2008, 10:38:18 AM2/27/08
to django...@googlegroups.com
Hey kevinski,
 
Thanks for ur suggestion..
 
Can u please explain a bit...this can be a gud solution for me...Can u please eplain a bit ;)

gordyt

unread,
Feb 27, 2008, 10:43:06 AM2/27/08
to Django users
Hi rajivb,

I was in a spot where I had to tie an existing PHP site in with a
Django site. I did not want folks to be able to access the PHP part
of the site unless the following conditions applied:
(1) they were logged in with the Django app, and
(2) they did NOT belong to the "Sales" group

Here is the PHP code that I used to implement that:

---------- cut here ----------
<?php
require(dirname($_SERVER['DOCUMENT_ROOT']) . "/config/db.php");
$authenticated = false;
if(array_key_exists('sessionid',$_COOKIE)) {
$sessionid = $_COOKIE['sessionid'];
$dbj = get_django();
$result = $dbj->query("select * from django_session where
session_key='$sessionid'");
if($row = $result->fetch(PDO::FETCH_ASSOC)) {
$raw_data = base64_decode($row['session_data']);
$raw_arr = explode('_auth_user_id', $raw_data);
$len = ord(substr($raw_arr[1],3,1));
$dec = 0;
while($len > 0) {
$dec <<= 8;
$byte = ord(substr($raw_arr[1],3+$len,1));
$dec |= $byte;
$len -= 1;
}

$result = $dbj->query("select is_active from auth_user where
id=$dec");
if($row = $result->fetch(PDO::FETCH_ASSOC)) {
if($row['is_active']) {
$result = $dbj->query("select count(*) from
auth_user_groups t1 left join auth_group t2 on t1.group_id=t2.id" .
" where t1.user_id=$dec and t2.name='Sales'");
$row = $result->fetch(PDO::FETCH_NUM);
if($row[0] == 0) {
$authenticated = true;
}

}
}
}
else {
$error_info = $result->errorInfo();
syslog(LOG_DEBUG, sprintf("hbadmin/index.php: SQLSTATE=%s,
error_code=%s, error_message=%s",
$error_info[0], $error_info[2], $error_info[2]));
}
}

if(!$authenticated) {
header("Location: /accounts/login?next=/hbadmin/");
exit;
}

?>
---------- cut here ----------

A few notes...

The get_django() function (defined elsewhere) returned a PHP PDO
object to my Django database. Basically the code makes sure they have
a Django session. It grabs their session data, which is a base64
encoded pickled object. It's not easy to decode a pickled python
object in PHP, but I decode just enough to grab the user id of the
logged in user.

By the way, this is on a secure site and both PHP and Django are
configured to use secure cookies. Also I really need to update that
first query so that an expired session key cannot be returned.. Right
now that app is in-house only and I will update it before it is
online.

--gordy

rajiv bammi

unread,
Feb 27, 2008, 10:50:37 AM2/27/08
to django...@googlegroups.com
Hi Gordy,
 
Thanks a lot for quick and very helpful response..It seems that u solved my problem ;)
But becuase i m just newer to Django and python i just need to thnk for a while and have to see whether things in reality works for me and not ;-)
 
By the way i thnk ur inputs really are very useful for me at this point of time ;-)
Thanks again dear ;)
 
 
Byeee
rajivb

kevinski

unread,
Feb 27, 2008, 10:59:40 AM2/27/08
to Django users
The iframe, or inline frame, is an html tag that seamlessly embeds one
page inside another. Very simple. Learn how to use here:
http://www.w3schools.com/tags/tag_iframe.asp

You will be able to embed a PHP page inside your Django template with
the following code.

<iframe src="/your_php_page.php" width="100%">
</iframe>

You can host your PHP pages the same way you host media (if you are
using Apache).

<Location "/php_pages">
SetHandler None
</Location>

I hope that helps :)

Kevin

rajiv bammi

unread,
Feb 27, 2008, 11:01:50 AM2/27/08
to django...@googlegroups.com
yup ;-)
thnx a lot dear

rajiv bammi

unread,
Feb 27, 2008, 11:50:16 AM2/27/08
to django...@googlegroups.com
Hey i thnk a lot on this code....i need to do almost the same thing..but i didnt get how u intergate PHP with django.
Is it by this line??
 $dbj = get_django();
 
if yes , then so my confusion is get_django(); is a funtion written in php..how it will integrate with django ..
 
i need to do almost same thing wat u did..like i need to show some pages to the user only if they r authrized by django app..
So i got stuck at that how i cn integrate this appl with my PHP code..
 
Can u please explain a bit ...
 
Thanks a lot for ur concerns and suggestions.. ;)

On Wed, Feb 27, 2008 at 9:13 PM, gordyt <gor...@gmail.com> wrote:

rajiv bammi

unread,
Feb 27, 2008, 12:02:45 PM2/27/08
to django...@googlegroups.com
Hi Gordy,
 
Hey i thnk a lot on this code....i need to do almost the same thing..but i didnt get how u intergate PHP with django.
Is it by this line??
 $dbj = get_django();
 
if yes , then so my confusion is get_django(); is a funtion written in php..how it will integrate with django ..
 
i need to do almost same thing wat u did..like i need to show some pages to the user only if they r authrized by django app..
So i got stuck at that how i cn integrate this appl with my PHP code..
 
Can u please explain a bit ...
 
Thanks a lot for ur concerns and suggestions.. ;)


 

gordyt

unread,
Feb 27, 2008, 4:23:31 PM2/27/08
to Django users
In my case I am running a very current version of PHP and I have
packges installed that let me access my Django database. For this
particular app the DJango database is using MySQL and the get_djang()
line is a PHP function, defined elsewhere, that returns a PDO object.

You of course do not have to use PDO at all, you can use the MySQL-
specific database functions if you like. But, assuming that you DO
want to use PDO:

/**
* Connect to django database.
* Returns:
* A PDO object
* Throws:
* PDOException - if connection error occurs
*/
function get_django() {
global $DJANGO_HOST, $DJANGO_NAME, $DJANGO_USER, $DJANGO_PW;
$dsn = sprintf("mysql:host=%s;dbname=%s", $DJANGO_HOST,
$DJANGO_NAME);
return new PDO($dsn, $DJANGO_USER, $DJANGO_PW);
}

--g

rajiv bammi

unread,
Feb 27, 2008, 11:40:54 PM2/27/08
to django...@googlegroups.com
Hey Gordyt,

Thanks a lot dear ..You made my day ;-) i thnk i got the solution ;-)

Thanks again..

Tk cr..
Byyee

Shinmaikeru

unread,
Feb 28, 2008, 6:40:14 PM2/28/08
to Django users
Thanks for this thread and the solution. I too have a large PHP app
which is built on outdated ERW framework and has been heavily modified
and cludged so that it is like yarn and spaghetti mixed. I want to
redo it in Django, but there are so many custom reports and forms that
it will take a while, especially since I am just learning Django and
Python. If I can get away with making a base for the webapp which
incorporates the existing PHP reports and forms, I can then work
through them, replacing them with Django/Python code one by one.

Thanks again. I will look into this.

On Feb 28, 1:40 pm, "rajiv bammi" <bammi.ra...@gmail.com> wrote:
> Hey Gordyt,
>
> Thanks a lot dear ..You made my day ;-) i thnk i got the solution ;-)
>
> Thanks again..
>
> Tk cr..
> Byyee
>
Reply all
Reply to author
Forward
0 new messages