session_start problems when run process in parallel - php

I need that both iframes forms work independent (async) but when i submit both forms at the same time the second iframe report "Fatal error: Maximum execution time of 30 seconds exceeded in C:\wamp\www\iframe2.php on line 2" (when the iframe1 query is a long process) or the second iframe return data AFTER the first process is complete (when the iframe1 query is a short process).
Now, i need keep the session for validate user login.
i need your help, my friends! thanks
index.php
<html>
<head>
<title></title>
</head>
<body>
<iframe src="iframe1.php" width="300" height="400"></iframe>
<iframe src="iframe2.php" width="300" height="400"></iframe>
</body>
</html>
iframe1.php (return query results)
<?php
session_start();
if($_SESSION['user'])
$data="Valid user";
else
header("location: login.php");
set_time_limit(120);
require_once("config.php"); //db conections
if($_POST)
{
//query (long process)
$data.= ""; // concatenated string with query results
}
?>
<html>
<head>
<title></title>
</head>
<body>
<?php echo session_id();?>
<form method="post">
ini:<input type="text" name="var1" value="" /><br />
fin:<input type="text" name="var2" value="" /><br />
<input type="submit" value="Send" />
</form>
Result:<br />
<?php
if(isset($data))
echo session_id()."<hr>".$data;
?>
</body>
</html>
iframe2.php (only return 123456)
<?php
session_start();
if($_SESSION['user'])
$data="Valid user";
else
header("location: login.php");
if($_POST)
{
$data = "123456";
}
?>
<html>
<head>
<title></title>
</head>
<body>
<?php echo session_id();?>
<form method="post">
<input type="text" name="inpt" />
<input type="submit" value="frame2" />
</form>
Result:<br />
<?php
if(isset($data))
echo session_id()."<hr>".$data;
?>
</body>
</html>

Default PHP session have blocking file-access. That means as long as in your first script the session is still active, the second script's access to the session is blocked. PHP will wait until the session is accessible again.
The solution often is to keep the time-span short for the active period. Normally the sessions does not need to be active all the time.
You activate a session with session_start().
You deactivate a session with session_commit().
Locate the part of your script where you actually need an active session. Open it as late as possible (start) and close (commit) it as soon as possible.

Related

Clear the login, delete the session, and redirect back to the login page using isPostBack - PHP

I've spent a lot of time today researching this site for my solution but I have had no luck. I'm currently trying to learn php and working on my second project. I can only use PHP. I originally had my delete session and redirect in a separate logout.php file. This was working but then I found out that I can't do this. I've been instructed that I need to "clear the login, delete the session, and redirect back to the login page" and do this within an isPostBack in the results.php file. After a lot of research today I thought I was understanding how to do this but I can't get it to work. Hoping I can get some help.
<?php
session_start();
//require_once('cookies.php');
$isPostBack = filter_input(INPUT_GET, 'submit');
//this is where I need to do the isPostBack for user clicking "logout".
if ($isPostBack) {
// clear ALL session data from memory
// clean up the session and remove the session ID.
// redirect to index.php
endSession();
session_destroy();
header("Location: index.php");
} else {
// user did not click logout doNothing();
}
?>
<html lang="en">
<head>
<title>Results</title>
<link rel="stylesheet" type="text/css" href="">
</head>
<body>
<form action="results.php">
<input type="submit" id="submit" name="submit" value="Logout" />
</form>
<section>
<?php
foreach($_SESSION['answers'] as $answer){
echo "<p>$answer</p>";
}
?>
</section>
</body>
Try to provide name attribute
<input type="submit" id="submit" value="Logout" name="logout"/>
and use only logout variable in place of submit or provide two different fields
$isPostBack = filter_input(INPUT_GET, 'submit');
$isPostBack = filter_input(INPUT_GET, 'logout');
I seem to have found my solution. I needed to give the isPostBack variable a name that matched the name given to the logout button. I also needed to include !==NULL after the isPostBack. I changed endSession(); to $_SESSION = array(); According to my research, endSession(); "removes all session variables". It seems to be working as it should now. Here is my edited code.
<?php
session_start();
$isPostBack = filter_input(INPUT_GET, 'submit')!==NUll;
//this is where I need to do the isPostBack for user clicking "logout".
if ($isPostBack) {
// clear ALL session data from memory
// clean up the session and remove the session ID.
// redirect to index.php
$_SESSION = array();
session_destroy();
header("Location: index.php");
} else {
// user did not click logout doNothing();
}
?>
<html lang="en">
<head>
<title>Results</title>
<link rel="stylesheet" type="text/css" href="">
</head>
<body>
<form action="results.php">
<input type="submit" id="submit" name="submit" value="Logout" />
</form>
<section>
<?php
foreach($_SESSION['answers'] as $answer){
echo "<p> $answer</p>";
}
?>
</section>
</body>
If you need to remove se particular session values you can use unset()
unset ($_SESSION['userid'])

How to stop a session with a button?

I have a form and added a session_starts I want to track of how many times I have visited the page ..I want a Button indicating the stop session and the session should stop and start with a new session ...How do I do this?
Suggestion:
Try creating a button in a form and after submitting it change session values to what you want and the start another.
Example
<form method="POST">
<input type="submit" name="emptySession">
<form>
<?php
if(isset($_POST['emptySession']){
//you continue
}
?>
I'd have thought that something like the following ought to work- the logic can be adapted to suit whatever page you have.
<?php
session_start();
if( !empty( $_POST['reset'] ){
#session_unset();
#session_destroy();
#session_start();
#session_regenerate_id( true );
}
?>
<!doctype html>
<html>
<head>
<meta charset='utf-8' />
<title>Sessions</title>
</head>
<body>
<form method='post'>
<input name='reset' value='Reset' type='submit' />
</form>
</body>
</html>

Auto refreshing php page

I made this simple chat site for my website but I don't know how to make it auto refresh every time a message has been sent.
Site that sends and prints out all messages:
<form action="messages.php" method="POST">
<input name="chat_box" /><br>
<input type="submit" value="Send" />
</form>
<?php
include "messages.txt";
?>
Site that sends text input to a text file:
<?php
$messages = $_POST["chat_box"];
$handler=fopen("messages.txt", 'a');
fwrite($handler,$_SERVER["REMOTE_ADDR"].":".$messages."<br>");
fclose($handler);
header("Location: chat_box.php");
?>
Can anyone help me?
Try this code :
This is a messages.php :
<?php
$page = $_SERVER['PHP_SELF'];
$sec = "10";
?>
<html>
<head>
<meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
</head>
<body>
<form action="messages.php" method="POST">
<input name="chat_box" /><br>
<input type="submit" value="Send" />
</form>
<?php
include "messages.txt"; //Uncomment this to check the autorefresh
echo "Auto refresh in 10 second!";
$messages = $_POST["chat_box"];
$handler=fopen("messages.txt", 'a');
fwrite($handler,$_SERVER["REMOTE_ADDR"].":".$messages."<br>");
fclose($handler);
?>
</body>
</html>
Hope this help you out... :)
If you mean to get new messages, your best bet is probably to re-load the text file every 10 seconds. to do so replace the php in the bottom of your 1st code set with this:
<div id="messages"></div>
<script type="text/javascript">
$(document).ready(function() {
function functionToLoadFile(){
jQuery.get('messages.txt', function(data) {
$("#messages").html(data)
});
}
setInterval(functionToLoadFile, 10000);
});
</script>

Login functionality in HTML page

I have created a HTML page which takes user-id and password from user and then check there validity through database. Till now i was directing them to another page after successful login. But now i want to update same page after login. Just like www.facebook.com ; when we are NOT logged in its asks for user-id and password, but if we are login our profile contents are displayed on the same page i.e. facebook.com. What i was doing; directing it to page "login.php" which of course you can access without login.
For example there is a page "movies.com" which allows user to watch some movies after login; before i was just directing them to another page say "successful_login.com" after they login. It was a funny approach, but was working for my college assignments.
PS. Am just a noob, sorry if i asked something funny.
<?php
if(mysql_connect("localhost","root","")==false)
{
die ("Connection Failed");
}
mysql_select_db("data");
if($_POST)
{
$id=$_POST["email"];
$pwd=$_POST["password"];
$pwd=hash( 'sha256', $pwd);
$sql=mysql_query("SELECT* FROM admin_data WHERE id='$id' AND pass='$pwd'");
if($sql)
{
header("Location: login.php");
}
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset="UTF-8" />
<title>
HTML Document Structure
</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<form method="POST">
<h1>Welcome</h1>
<div class="inset">
<p>
<label for="email">Login</label>
<input type="text" name="email" id="email">
</p>
<p>
<label for="password">PASSWORD</label>
<input type="password" name="password" id="password">
</p>
</div>
<p class="p-container">
<span>Forgot password ?</span>
<input type="submit" name="Login" id="Login" value="Log in">
</p>
</form>
</body>
</html>
To use the session variable you need to start session at the top.
session_start();
Now store the email value in the session in here.
if(mysql_num_rows()>0)//It was originally if($sql)but I am using mysql_num_rows
//The reason for saving the value in the session here is this.
First you want to make sure that user have valid credential to log in.
{
$_SESSION['email']=$id
header("Location: login.php");
}
In your form you can do something like this
session_start();//Start the session at the top so you can use the session variable.
then simply use if else statement.
if($_SESSION['email']==TRUE)
{
$email=$_SESSION['email'];
//Now you can run the query by using $email to fetch the record of the user.
}
else
{
//Show them a form or redirect them to another page.
}
Note:mysql is deprecated and is going to be dropped soon. Use mysqli or P.D.O

Simple PHP Cookie

I am experimenting with cookies and i am doing this quick example,
<html>
<head>
<meta charset="UTF-8">
<title>Cookies</title>
</head>
<body>
<!-- Start of FORM -->
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Username: <input type="text" name="username"><br>
<input type="submit" name="submit" value="Submit">
</form>
<!-- End of FORM -->
<hr>
<?php
if (isset($_POST['username'] )) {
setcookie('username', $_POST['username'], time() + 1000, '/');
if(isset($_COOKIE['username'])){
echo "Hello " . $_COOKIE['username'];
unset($_COOKIE['username']);
}
}
?>
</body>
It works but i have to click the submit button twice for my message to display, why is that?
From the PHP Docs..
Cookies will not become visible until the next loading of a page that
the cookie should be visible for. To test if a cookie was successfully
set, check for the cookie on a next loading page before the cookie
expires.
So whilst you clicked the button the second time, the actual load was in effect and you were able to see it(the cookie).

Categories