Forum Moderators: coopster

Message Too Old, No Replies

PHP in use with javascript

Is it possible to perform the following.

         

realitybytesuk

7:15 pm on Apr 8, 2005 (gmt 0)

10+ Year Member



I have hit a bit of a dilema with a script I am writing.

The task at hand is, I have a login form, that needs to be authenticated against a database, password is md5.

If the authentication passes, I need to create a cookie with username md5 password and one other detail, exp time, and location to /. Then reload the page.

Question is it possible to execute javascript depending on the outcome of the php authentication?

Question is it possible to pass the variables I use in php to authenticate over to javascript?

Question is it possible to reload the page with javascript upon success?

*edit* I think I already no the answer after reading it back a few times, server/client side scripting languages comes to mind.

Guess I will have to learn javascript set the cookie reload and let the authentication at the page beginning deal with it.

mcibor

10:37 am on Apr 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Dear realitybytesuk!

As you know already:) It is possible to execute different javascripts depending on the authentication result.

<?php

$auth = authentification($name, $pass); //your function that returns true/false, or whatever

if($auth === true) include("true.html");
else include("false.html");
?>

In true.html you can have your function, however better would be to include just the desired js:

<script type="text/javascript" language="Javascript" src="
<?php
if($auth === true) echo "true.js";
else echo "false.js";
?>
"></script>

mcibor

10:38 am on Apr 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Passing the auth vars to js is done by writing js
<script bla bla
var name = "<?php echo $name;?>";

To redirect (reload)
<script bla bla
rtime = 5000; //miliseconds
rurl = "index.html";
function redirect()
{
self.setTimeout("self.location.href=rurl;", rtime);
}

Then onload="redirect()"

However with cookies you can set them with php as well: see the setcookie section at [php.net...]

Best regards
Michal Cibor

BTW Welcome to webmasterworld!

realitybytesuk

2:54 am on Apr 10, 2005 (gmt 0)

10+ Year Member



Hi, thank you for your input.

I have managed to solve my dilema without the use of javascript.

The problem I encountered was due to the fact that, I have built my site around a forum, which I did not write. The forum was everything I was looking for very simple in features and highly customisable. Being several weeks in to the site setup and design, I was not really willing to implement another forum.

The forum uses a cookie authentication, and with it only being one page and many templates it is feasable. So I had to base my whole site authentication around this cookie.

I am farily new to php and was not aware how strict php was when it came to setting cookies, I had wrote down the code for the authentication thinking I would be able to set the cookie with php at any point and not just before the headers have been sent.

Using the forum to login and test my own code, I eventually got to the login form and performed the auth, set the cookie to find that my session start at the top of the page prevented the cookie being set. Another problem I encoutered because the login is apart of the same page and it reloads the page, the following authentication functions did not detect the cookie until the page had been reloaded once over. This was not good, as my whole authentication is based around detecting the cookie Arghhh lol.

Anyway I have managed to bypass it by authenticating above the headers, setting various variables to meet the desired outcome, then on following page loads, the original auth kicks in and all is good.

2 days in the life of a fairly new programmer, this is actually the first script I have wrote without downloading/finding some code I have needed because I didnt fully understand what I was doing.

Needless to say I am very happy with the outcome, it is a great feeling when you set yourself a task, and reach that goal in programming.

rojer_31

10:42 am on Apr 10, 2005 (gmt 0)

10+ Year Member



Good for you, realitybytesuk, I know what you mean!