Forum Moderators: coopster

Message Too Old, No Replies

Javascript inside PHP

How to make it work

         

Francis

10:59 am on Feb 13, 2004 (gmt 0)

10+ Year Member



<?php
session_start(); require_once($_SERVER['DOCUMENT_ROOT'].'/settings.php');

// Checks if the member_name and mem_pass has already been registered, redirect if not.

if (!isset($_SESSION['member_name']) &&!isset($_SESSION['mem_pass'])) {
$back_to_this_url = ($_SERVER['REQUEST_URI']);
$_SESSION['back_to_this'] = $back_to_this_url;

header("Location:" . SITE_ROOT . "restricted_resource.php");

} // close if (!isset...
?>

<HTML>
.
.
.
.
</HTML>

Question:

How do I change the header() part to this Javascript:

<SCRIPT language="JavaScript" type="text/javascript">
<!--
location.href="http://localhost/restricted_resource.php"
//-->
</script>

Actually, the header is already working and the script redirects to the restricted_resource.php. However, I am encountering problems with another script because of the header() command. So, I was wondering as to whether how to make the JavaScript run because when I tried this:

$url = (SITE_ROOT . "restricted_resource.php");
echo("<script language=\"JavaScript\" type=\"text/javascript\">");
echo("<!--");
echo("location.href=\"http://localhost/restricted_resource.php\"") ;
echo("// -->");
echo("</script>");

inserted in place of the header() command, it is not working.

Any advice or alternative solutions?

Sorry I know that this is kinda newbie, but, well, I am.

scumm_bar2

11:59 am on Feb 13, 2004 (gmt 0)

10+ Year Member



Why do you need header()?

From what I can see all you need is

if (!isset...)
{ // not registered, use javascript to redirect
echo '<script type="text/javascript">'."\n";
echo '<!--'."\n";
echo 'location.href="http://localhost/restricted_resource.php"'."\n";
echo '// -->'."\n";
echo '</script>'."\n";
}

vincevincevince

12:01 pm on Feb 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



try putting it as <body onLoad="javascript: [your script here];"></body>

Francis

12:05 am on Feb 15, 2004 (gmt 0)

10+ Year Member



Hey, thanks that worked well. Exactly what I needed.