PHP - Log Out When Browser Is Closed
OK, I'm nearing the end of a very long chat project and I have one last hurdle to jump, I've saved this task to the near end because I thought it would be a problem as I have no idea what to do or where to start. The problem is exactly what is said in the subject title.
I have looked into JavaScript options such as "onbeforeunload" but that is just BAD. The login is controlled by sessions, so when the user closes the browser, the session is ended, but data in the database says different, cookies are not an option I'm afraid. I've had an idea that if I create a special account (or a few) and always leave these accounts logged in, they can check if a user has been inactive for a certain period of time, and then run queries to change things, I know this sound odd but maybe I will not have to create these accounts but general accounts that people make will be able to do this for me, without them even knowing. Please help, any ideas, suggestions, logic or methods that you think will help will be great. Thanks. Similar TutorialsHey All, I have a sessions table that is used to see if a user is online. It only gets deleted when they click the Logout button. What I need to do is have it so it gets deleted even if they close the browser. Should I add a column in the sessions table with a time stamp and check on each page for every user to make sure its great than 30 min? Is there a better way to do this, without so many queries? Thanks Hello, How do I get a user's session destroyed when he closes his browser? Thanks in advance I made a bug which caused endless loop and endless printing. When I noticed that, I closed the browsers tab, fixed the bug, open a new tab and re-run. I was assuming the server would start from the beginning, but instead I noticed that some variables were active and the program continued from where it stopped. How can I make the program to stop when the client tab is terminated? This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=352282.0 Hello dear friends, let say we have (function) and that function has one variable which is (id) the (id) could be number any number 1 or 4 or even 49999 whatever. how to make closed loop which i mean run this function in unlimited way each time with new id <--- i can do it by set random() but the problem i've is how to make it repeat itself excuted excuted excuted | | | ect let say for example , i will insert random number in at database table Code: [Select] $id = rand(); mysql_query("INSERT INTO comments(id) VALUES ('$id')"); that is it, how then i can repeat it many many times without 1) refresh 2) rewrite it many times thanks in advance I have a class I use to handle my various DB objects. I want to close any connections, if needed, in my destructor. I want to do this even though I already know the connection has been closed previously. What I am looking for is a way to test the $connection variable I have to see if it's in need of closing. I can't figure out how to do this. if (isset($this->connection)) { echo "is set"; } if ($this->connection) { echo "here"; } The above would output "is set" and "here", because in both tests say the already closed connection is still there. Can anyone offer any help? Ok this is some noob question here I have a page that has few separate pieces of php code embeded within html code. Each piece is separated with Code: [Select] <?php ... ?> tags, everything works fine. I need to assign a variable in first (from the top) piece of php code. Then I want to be able to use that variable in all others php pieces across this same page.... I searched and searched and first tried to do it like : First piece: Code: [Select] <?php function id() { $id="blah"; } ?> Second piece : Code: [Select] <?php id(); ... echo $id; ?> didn't work, then I tried First place: Code: [Select] <?php $id="blah"; function id() { global $id; } ?> Second piece : Code: [Select] <?php echo $id; ?> After struggling and reading for few days all about global, static, and regular variables i finally decided to ask professionals to help.... please let me know if you need me to make the question description more clear or smn i have a script that wrote which updates fields in a mysql database with millions of rows. it just uses a while loop for each row. now the problem is that i forgot to add something to the script, but i already visited the .php file in my browser. i closed the browser and when i refresh the database it still seems to be running even though i closed the browser the script was running on.... is this because the script is still cached in the memory or what? how can i stop it? Hi Everyone, I wonder if somebody could help me. I have been at this for hours and am really struggling. I have a timer which I have set up for a quiz. The quiz opens in a javascript shadow box. When the quiz is opened a session variable for the timer is set up to keep track of the time. What I need is for this session variable to end when the shadow box is closed. I have a onClose hook to peform actions when the shadow box is closed which is in javascript. Obviously I cannot end the session variable in the javascript function due to js being client side and php being server side. Has anyone any suggestions how I can resolve this issue. I need to end this session variable as if the user exits the quiz and then selects another quiz to do the timer uses the current time session rather than assigning the new time value for that particular quiz. Thanks in advance. Edd Hello. I want to display all photos from the database. The problem is that it displays nothing. I added a foreach loop in html but further it not display. I'am beginner, I hope someone help me where I make mistake. I care about separation logic from view.
class photo_display { private $database; private $userData; public function __construct($database) { $this->database = $database; } public function display() { $query = $this->database->connect()->prepare("SELECT photo FROM photo"); $query->execute(); if ($query->rowcount()) { $row = $query->fetchAll(); } $this->userData = $row; } public function display_Photos(string $colName) { return (isset($this->userData[$colName])) ? $this->userData[$colName] : []; } } <div class='container-fluid bg-dark' id='profil'> <div class='row'> <div class=" col-12 col-sm-12 col-md-4 col-lg-4 col-xl-4 order-5 order-12 order-6 order-md-4"> <div class="row" id="margin"> <div class="col-xl-12 d-flex justify-content-center" id="test"> <?php foreach ($photo_display->display_Photos('photo') as $value) { ?> <img src ="<?php echo var_dump($value) ?>" />; <?php } ?> </div> </div> </div>
I GET QuoteFatal error: TPL: [in line 0]: syntax error: file 'm/pg/_categories' does not exist in C:\WebServ\httpd\libs\tpl\class.template.php on line 943 unset($_templatelite_tpl_vars); else: $_templatelite_tpl_vars = $this->_vars; echo $this->_fetch_compile_include($this->_vars['TPLx'].'m/pg/_categories'.$this->_vars['HTML'], array()); $this->_vars = $_templatelite_tpl_vars; WITH FILES http://testynarkotykowe.j13x.pl/index.txt && http://testynarkotykowe.j13x.pl/indexpg.txt HOW TO FIX IT Hello all, I'm an amateur at PHP coding, and am currently enrolled in a PHP and MySQL class that uses the PHP Programming with MySQL textbook, by Don Gosselin. I'm trying to get this simple Shopping Cart script to work, because it's the step-by-step example script for Chapter 11, but I keep getting the following errors on ShowCart.php when I try to add an item to the cart. Quote Warning: mysqli_query() [function.mysqli-query]: Couldn't fetch mysqli in C:\xampplite\htdocs\ShoppingCart.php on line 78 Warning: mysqli_errno() [function.mysqli-errno]: Couldn't fetch mysqli in C:\xampplite\htdocs\ShoppingCart.php on line 80 Warning: mysqli_error() [function.mysqli-error]: Couldn't fetch mysqli in C:\xampplite\htdocs\ShoppingCart.php on line 81 Line 78: $QueryResult = mysqli_query($this->DBConnect, $SQLstring) Line 80 and 81: . "<p>Error code " . mysqli_errno($this->DBConnect) . ": " . mysqli_error($this->DBConnect)) . "</p>"; I've read "Couldn't fetch mysqli" errors are because the connection was closed prior to those lines, but I don't see any indication of a connection closure. I have no idea what to do, as all the code was given to me in the book, I merely copied it down. Here's the ShoppingCart.php code:<?php class ShoppingCart { private $DBConnect = ""; private $DBName = ""; private $TableName = ""; private $Orders = array(); private $OrderTables = array(); function construct() { $this->DBConnect = mysqli_connect("localhost", "root", "passHere"); if (mysqli_connecT_errno()) die("<p>Unable to connect to the database server.</p>" . "<p>Error code " . mysqli_connect_errno() . ": " . mysqli_connect_error()) . "</p>"; } public function setDatabase($Database) { $this->DBName = $Database; $this->DBConnect->select_db($this->DBName) Or die("<p>Unable to select the databbase.</p>" . "<p>Error code " . mysqli_errno($this->DBConnect) . ": " . mysqli_error($this->DBConnect)) . "</p>"; } public function setTable($Table) {echo $table."<br />"; $this->TableName = $Table; } public function getProductList() { $SQLstring = "SELECT * FROM $this->TableName"; $QueryResult = $this->DBConnect->query($SQLstring) Or die("<p>Error code " . mysqli_errno($this->DBConnect) . ": " . mysqli_error($DBConnect)) . "</p>"; echo "<table width='100%' border='1'>"; echo "<tr><th>Product</th><th>Description</th><th>Price Each</th><th>Select Item</th></tr>"; $Row = $QueryResult->fetch_row(); do { echo "<tr><td>{$Row[1]}</td>"; echo "<td>{$Row[2]}</td>"; printf("<td align='center'>$%.2f</td>", $Row[3]); echo "<td align ='center'> <a href='ShowCart.php?PHPSESSID=" . session_id() . "&operation=addItem&productID=" . $Row[0] . "'>Add</a></td></tr>"; $Row = $QueryResult->fetch_row(); } while ($Row); echo "</table>"; } public function addItem() { $ProdID = $_GET['productID']; if (array_key_exists($ProdID, $this->Orders)) exit("<p>You already selected that item! Click your browser's back button to return to the previous page.</p>"); $this->Orders[$ProdID] = 1; $this->OrderTable[$ProdID] = $this->TableName; } function _wakeup() { $this->DBConnect = new mysqli("localhost", "staticlo_shane", "shinfoosh"); if (mysqli_connect_errno()) die("<p>Unable to connect to the database server.</p>" . "<p>Error code " . mysqli_connect_errno() . ": " . mysqli_connect_error()) . "</p>"; $this->DBConnect->Select_db($this->DBName) Or die("<p>Unable to select the database.</p>" . "<p>Error code " . mysqli_errno($$this->DBConnect) . ": " . mysqli_error($this->DBConnect)) . "</p>"; } public function showCart() { if (empty($this->Orders)) echo "<p>Your shopping cart is empty!</p>"; else { echo "<table width='100%' border='1'>"; echo "<tr><th>Remove Item</th><th>Product</th><th>Quantity</th><th> Price Each</th></tr>"; $Total = 0; foreach($this->Orders as $Order) { $SQLstring = "SELECT * FROM " . $this->OrderTable[key($this->Orders)] . " WHERE productID='" . key($this->Orders) . "'"; $QueryResult = mysqli_query($this->DBConnect, $SQLstring) Or die("<p>Unable to perform the query.</p>" . "<p>Error code " . mysqli_errno($this->DBConnect) . ": " . mysqli_error($this->DBConnect)) . "</p>"; $Row = mysqli_fetch_row($QueryResult); echo "<td align='center'>"; echo "<a href='ShowCart.php?PHPSESSID=" . session_id() . "&operation=removeItem&productID=" . $Row[0] . "'>Remove</a></td>"; echo "<td>{$Row[1]}</td>"; echo "<td align='center''>$Order "; echo "<a href='ShowCart.php?PHPSESSID=" . session_id() . "&operation=addOne&productID=" . $Row[0] . "'>Add</a>"; echo "<a href='ShowCart.php?PHPSESSID=" . session_id() . "&operation=removeOne&productID=" . $Row[0] . "'>Remove</a>"; echo "</td>"; printf("<td align='center'>$%.2f</td></tr>", $Row[3]); $Total += $Row[3] * $Order; next($this->Orders); echo "<td align='center' colspan='2'><strong>Your shopping cart contains " . count($this->Orders) . " product(s).</strong></td>"; printf("<td align='center'><strong>Total: $%.2f</stong> </td>", $Total); echo "</table>"; } echo "<tr><td align='center'><a href='ShowCart.php?PHPSESSID=" . session_id() . "&operation=emptyCart'><strong> Empty Cart</strong></a></td>"; } } public function removeItem() { $ProdID = $_GET['productID']; unset($this->Orders[$ProdID]); unset($this->OrderTable[$ProdID]); } function emptyCart() { $this->Orders = array(); $this->OrderTale = array(); } function _destruct() { $this->DBConnect->close(); } public function addOne() { $ProdID = $_GET['productID']; $this->Orders[$ProdID] += 1; } public function removeOne() { $ProdID = $_GET['productID']; $this->Orders[$ProdID] -= 1; if ($this->Orders[$ProdID] == 0) $this->removeItem(); } } ?> Here is the ShowCart.php code:<?php session_start(); require_once("ShoppingCart.php"); if (!isset($_SESSION['curCart'])) header("location:GosselinGourmetGoods.php"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta name="Robots" content="nofollow, noindex" /> <link rel="stylesheet" type="text/css" media="screen" href="php_styles.css" /> </head> <body> <h1>Gosselin Gourmet Goods</h1> <h2>Shop by Category</h2> <p><a href="GosselinGourmetCoffees.php">Gourmet Coffees</a><br /> <a href="GosselinGourmetOlives.php">Specialty Olives</a><br /> <a href="GosselinGourmetSpices.php">Gourmet Spices</a></p> <?php $Cart = unserialize($_SESSION['curCart']); if (isset($_GET['operation'])) { if ($_GET['operation'] == "addItem") $Cart->addItem(); if ($_GET['operation'] == "removeItem") $Cart->removeItem(); if ($_GET['operation'] == "emptyCart") $Cart->emptyCart(); if ($_GET['operation'] == "addOne") $Cart->addOne(); if ($_GET['operation'] == "removeOne") $Cart->removeOne(); } $Cart->showCart(); $_SESSION['curCart'] = serialize($Cart); ?> </body> </html> Here is the product page for "Specialty Olives" which is identical to the Coffees and Spices pages, save for the Table name changed to their respective products: <?php session_start(); require_once("ShoppingCart.php"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Gosselin Gourmet Goods</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta name="Robots" content="nofollow, noindex" /> <link rel="stylesheet" type="text/css" media="screen" href="php_styles.css" /> </head> <body> <h1>Gosselin Gourmet Goods</h1> <h2>Shop by Category</h2> <p><a href="GosselinGourmetCoffees.php">Gourmet Coffees</a><br /> <a href="GosselinGourmetOlives.php">Specialty Olives</a><br /> <a href="GosselinGourmetSpices.php">Gourmet Spices</a></p> <h2>Speciality Olives</h2> <?php $Database = "gosselin_gourmet"; $Table = "olives"; $Cart=!empty($_SESSION['curCart'])?unserialize($_SESSION['curCart']):new ShoppingCart(); $Cart->construct(); $Cart->setDatabase($Database); $Cart->setTable($Table); $Cart->getProductList(); $_SESSION['curCart'] = serialize($Cart); ?> <p><a href='<?php echo "ShowCart.php?PHPSESSID=" . session_id() ?>'>Show Shopping Cart</a></p> </body> </html> I'd like to use an application that's not browser based, on Internet Explorer. I've read somewhere that this is possible with a PHP script, but which one? If you can get me on the right track, I'd really appreciate it. Cheers. Hey there, I'm trying to set up a little ftp script to make it easier to upload files and add new folders for images and videos ect on a backend admin page however the following script just will not connect I keep getting the "Couldn't connect to 66.40.52.167" error so it's not even finding the server however when I put it into my browser it connects just fine, I've even pinged it and all works fine so I don't get it. $ftp_server = "66.40.52.167"; $ftp_user = "username"; $ftp_pass = "password"; $conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) { echo "Connected as $ftp_user@$ftp_server\n"; } else { echo "Couldn't connect as $ftp_user\n"; } ftp_close($conn_id); Any ideas would be appreciated! I am trying to come up with a script that will detect whether the requesting user agent is a bot or a browser, as their are so many bots and scripts around i have decided to go with the latter so i need a list of browsers that may be used to do the following Quote $browser_array = array(list of browsers..................); if(!in_array($_SERVER['HTTP_USER_AGENT'], $browser_array)) { output text; } else { output html; } so basically i am now stuck without a complete list of user agents so if anyone knows where i can find a list it would be much appreciated Hello,
I want to know how they keep the same url when page is changing, if you dont know what i mean try the link and check the browser when you wat happens when you click in a other link in the website.
( Website Link ( Normal Content ) ) http://geedmo.com/?item=LushSlider
Thanks in advance.
Hello PHPers, I need help regards obtaining the correct Host and IP address. Using all browsers except mozilla-firefox, I will received the correct IP address. But with Mozzilla-firefox I get the wrong Host and IP address back as 10.1.23.22 ..The code I have is $mj_ip = $_SERVER['REMOTE_ADDR']; $mj_host = gethostbyaddr($_SERVER['REMOTE_ADDR']); Cheers, HJ Hi, I have tried a "Hello.php" to open in my web browser. It throws 404 file not found error. I am using Intranet. How to determine the web server I am using? Thanks. I need to use an if statement in conjunction with the browser name/type "Mozilla Firefox" vs "Google Chrome" to display certain scripts of CSS to a page. I am not sure how to do this. Any help would help, Brian This topic has been moved to HTML Help. http://www.phpfreaks.com/forums/index.php?topic=314530.0 |