Open new window and auto-close the window

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Open new window and auto-close the window

Post by tristanlee85 »

As we all here are familiar with phpBB, you know how when you get a PM a little window pops up, and after you click on the link, the window closes and the page loads in the "main" original window? Well, I'm trying to do something similar on my site. Here's the scenerio:

A user is currently on index.php and is logging in. The information is posted to user_login.php which is to open in a new window and display "Thank you for logging in, $username" After 2 seconds, I'd like the pop-up window to close and then have index.php reload itself.

I'm really not sure where to begin. I have some Javascript, but it isn't working too well.

Here is my user_login.php page that receives the login information and processes everything from there. This is the window that opens as a pop-up and is to close after 2 seconds and load index.php?mode=main in the main window:

Code: Select all

<?php
include('include/remote.php');

$user = $_POST['user'];
$pass = md5($_POST['password']);
$match = "SELECT user_id FROM phpbb_users WHERE username = '".$_POST['user']."'
and user_password = '".md5($_POST['password'])."';"; 

$qry = mysql_query($match)
or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry); 

if ($num_rows <= 0) { 
echo "<div align=\"center\">";
echo "Sorry, there is no username \"$user\" with the specified password. Please check your information.<br>";
echo "<a href=\"javascript: history.go(-1);\">Try again</a>";
echo "</div>";
exit; 
} else {

session_start();
$_SESSION['tsnhosting_username'] = $user;

//setcookie("loggedin", "TRUE", time()+(3600 * 24));
//setcookie("tsnhosting_username", "$user");

echo "<div align=\"center\">";
echo "<meta http-equiv=\"refresh\" content=\"2;URL=javascript:window.open('index.php?mode=main','_parent');\">Thank you for logging in, $user! Please wait...<br>";
echo "</div>";


}
//echo "</div>";

mysql_close();
?>
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Yeah whats you Javascript code then?
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

Well, assuming the user successfully logs in, this following Javascript is executed:

Code: Select all

echo "<div align=\"center\">";
echo "<meta http-equiv=\"refresh\" content=\"2;URL=javascript:window.open('index.php?mode=main','_parent');\">Thank you for logging in, $user! Please wait...<br>";
echo "</div>";
This is the actual Javascript:

javascript:window.open('index.php?mode=main','_parent');
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

ok then add some more features (http://www.javascripter.net/faq/openinga.htm) to make it a certain size, ect.

and then on the popup window what you need to do is make a script with a function that does this:


Code: Select all

<script>
function closeandrefresh(){
     opener.location = theurltogoto;
     window.close;
     self.close;
}
</script>
And make the a link on the page that has:

Code: Select all

<a href="closeandrefresh();">Text</a>

The only thing I am not sure why your doing it is the 2 second thing so i didn't include that
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

The 2 second thing was code I used from another script that actually was used because the meta-refresh just suited me there, but I needed something different for this site. I'll see what I can do with that code. Thanks.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Let me know what the outcome is, and explain the 2 second thing a little more in depth.
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

Well, I did what you said and when I clicked on the link to call the function, it tried to direct me to http://24.95.42.77:98/tsnupload/closeandrefresh();

Bascially, when a user logs in, I want it to pop up a window that tells them that they have successfully logged in. After 2 seconds, I want wanting that pop-up to close and simply re-load the main page since more functions will be available when they are logged in.

The reason why I am opening a pop-up window is because I will be adding functions in there later on and removing the 2 second close delay, but for right now I'd just like to get the script working correctly and auto-close after 2 seconds.

My pop-up page (user_login.php) is currently this:

Code: Select all

<?php
include('include/remote.php');

$user = $_POST['user'];
$pass = md5($_POST['password']);
$match = "SELECT user_id FROM phpbb_users WHERE username = '".$_POST['user']."'
and user_password = '".md5($_POST['password'])."';"; 

$qry = mysql_query($match)
or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry); 

if ($num_rows <= 0) { 
echo "<div align=\"center\">";
echo "Sorry, there is no username \"$user\" with the specified password. Please check your information.<br>";
echo "<a href=\"javascript: history.go(-1);\">Try again</a>";
echo "</div>";
exit; 
} else {

session_start();
$_SESSION['tsnhosting_username'] = $user;

//setcookie("loggedin", "TRUE", time()+(3600 * 24));
//setcookie("tsnhosting_username", "$user");

echo "
<html>
<head>
<script language=\"javascript\">
	function closeandrefresh()
	{
		opener.location = index.php?mode=main;
     		window.close;
     		self.close;
	}
</script>
</head>
<body>
<p>Thank you for logging in, $user! <a href=\"closeandrefresh();\">Close</a><br></p>
</body>
</html> ";
}
//echo "</div>";

mysql_close();
?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

The code for the link should look like this

Code: Select all

<a href="javascript:closeandrefresh();">Text</a>
Post Reply