I can't update my session value
this is the code
<CENTER>
<?PHP
session_start();
$_SESSION['value'] = 15;
if(isset($_POST['submit'])){
$plus = 10;
$_SESSION['value'] = ($_SESSION['value'] + $plus);
}
echo "<FORM METHOD=post ACTION=\"?page=try&". time(). "\" NAME=try>\n";
echo "<br>";
echo "Your value :" .$_SESSION['value'];
echo "<INPUT TYPE=submit NAME=submit VALUE=\"Submit\"></FORM>";
?>
</CENTER>
the problem is everytime I click that button, it always give me the same result.
that $_SESSION['value'] is always 15 and it never changes
so how to update the session value ? so I will get $_SESSION['value'] become 35 (the past result is 25 ) when I click the button again
thanks
you explicitly set the session value to 15, and then add 10 to it. It should never reach 35. try removing the assignment to 15 (or better yet, adding an if(!isset($_SESSION['value'])) check before it) and then try again
You should also take John Conde's advice and make sure that you start the PHP session before sending any output. Please read the manual
session_start() has to be before any output is sent to the browser (when using cookie based sessions).
<?php session_start(); ?>
<CENTER>
<?php
$_SESSION['value'] = 15;
if (isset($_POST['submit'])) {
$plus = 10;
$_SESSION['value'] = ($_SESSION['value'] + $plus);
}
echo $_SESSION['value'];
echo '<form method="post" action="try1.php" name="try">' . "\n";
echo '<br>';
echo 'Your value :' . $_SESSION['value'];
echo '<input type="submit" name="submit" value="submit"></form>';
?>
</CENTER>
Related
I managed to get the user information using Google+ login.
if (isset($authUrl)) {
echo "<a href='" . $authUrl . "'><img src='img/google.png'></a>";
} else {
print "ID: {$id} <br>";
print "Name: {$name} <br>";
print "Email: {$email } <br>";
print "Image : {$profile_image_url} <br>";
print "Cover :{$cover_image_url} <br>";
print "Url: {$profile_url} <br><br>";
echo "<a class='logout' href='?logout'><button>Logout</button></a>";
}
This is in a login.php file where the user logs in. How can I pass this data to the header.php so I can remove the Login and Register buttons and add the user name and image?
To pass info via GET:
For redirecting in php, you use header('Location:file_name')
header('Location: header.php?var1=val1&var2=val2');
Session:
// index.php
session_start(); //start session
$_SESSION['varName'] = 'varVal'; //make a session variable
header('Location: header.php'); // redirect to header.php
// header.php
session_start(); //start session
$myVar = $_SESSION['varName']; //access your session here
Post: Take a look at this.
Another way is to include a hidden field in a form that submits to page two:
<form method="post" action="header.php">
<input type="hidden" name="varname" value="var_value">
<input type="submit">
</form>
And then on header.php:
//Using GET
$var_value = $_GET['varname'];
//Using POST
$var_value = $_POST['varname'];
//Using GET, POST or COOKIE.
$var_value = $_REQUEST['varname'];
Just change the method for the form to get if you want to do it via get.
Happy Coding! :)
I have a file "test.php" like this:
<?php
session_start();
unset($_SESSION['ph']);
require 'connection.php';
if(!session_id())
session_start();
else
{
echo session_id();
echo "<br>";
echo var_dump($_SESSION);
}
$ph = 0;
if(isset($_POST['ph']))
if(isset($_POST['submit']))
{
$ph = $_POST['ph'];
$q1 = "select PHONE_CUST_ID from PHONE where PHONE_NO = :ph_bv";
$prepare = oci_parse($conn, $q1);
oci_bind_by_name($prepare, ':ph_bv', $ph);
oci_execute($prepare);
$res = oci_fetch_array($prepare, OCI_ASSOC);
if(!$res)
header('Location:newcustomer.php');
else
header('Location:oldcustomer.php');
}
?>
<!doctype html>
<html lang="en">
<body>
<form method="POST" action="" id="custphoneform">
<label for="PhoneNumber">Enter Phone Number:</label>
<input type="number" name="ph" required>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
The "oldcustomer.php" and "newcustomer.php" have similar code as below:
<?php
require 'connection.php';
session_start();
if(isset($_SESSION['ph']))
{
echo $_SESSION['ph'];
}
else
echo "Not found";
?>
The session variable is not working, gives me the following error:
Notice: Undefined index: ph in D:\xampp\htdocs\myfiles\newcustomer.php on line 4
The session works properly if I set the "form action" to some value but I don't want that. I want redirection based on user input. How to achieve it?
Example:
"test.php"
Enter Phone Number: 100
Based on the above value i.e. 100 I want to direct the user to different pages. If the number exists in the DB then oldcustomer.php otherwise newcustomer.php.
How can I do this?
You didn't give any value for action attribute here, So it will always post the data on current page only which is test.php. So you are getting error at oldcustomer.php when you are trying to access the variable.
<form method="POST" action="" id="custphoneform">
either you can directly post your data to oldcustomer.php by specifying it in action attribute as
<form method="POST" action="oldcustomer.php" id="custphoneform">
or you should store your data in $_SESSION global variable so you can access it anywhere in your project as
$_SESSION['ph'] = $_POST['ph'];
use this variable in oldcustomer.php as
<?php
session_start();
if(isset($_SESSION['ph'])){
echo $_SESSION['ph'];
}else{
echo "Phone number not found";
}
?>
You are redirecting after form post. At that time new request will be initialize which doesn't contain post data. Whsat you can do is in test.php assign session which will then use in any page
test.php
<?php
session_start();
require 'connection.php';
$ph = 0;
if(isset($_POST['ph']))
if(isset($_POST['submit']))
{
$ph = $_POST['ph'];
$q1 = "select PHONE_CUST_ID from PHONE where PHONE_NO = :ph_bv";
$prepare = oci_parse($conn, $q1);
oci_bind_by_name($prepare, ':ph_bv', $ph);
oci_execute($prepare);
$res = oci_fetch_array($prepare, OCI_ASSOC);
if(!empty($_POST['ph']))
$_SESSION['ph'] = $_POST['ph']; //assign value to session
if(!$res)
header('Location:newcustomer.php');
else
header('Location:oldcustomer.php');
}
?>
oldcustomer.php and newcustomer.php
<?php
session_start();
echo $_SESSION['ph'];
?>
you redirect to xxxcustomer.php (by the line header('Location: ...);. That will be a GET request.
So $_POST is empty.
So the if() will not be executed. { } are missing so the echo line will be executed next.
But no $_SESSION['ph'] is set >>> warning missing index ph
edit
I suppose you better do an include() on those xxxcustomer.php files?
EDIT 2
As you changed your code in the question, this answer makes no sense anymore.
Bottom line remains: after a redirect you loose the values in $_POST as it no longer is a POST-request
This should be an easy one, if I explain it well.
I am trying to create a web page, where the session starts when the user enters a name, the name is displayed constantly, until the user presses the log out button; which should end the session. The closest I have gotten to is this:
<if (isset($_POST['name'])) {
$_SESSION['name'] = $_POST["name"];
echo "<input type='submit' name='logout' method='logout' value='Log out'>";
echo "</form>";
echo "</h1>";
echo "</div>";
if(isset($_POST['logout']) && ($_POST['logout'] == "Log out")) {
session_destroy(); }
but it doesn't end the session and the name keeps being displayed constantly once entered. i.e. There is no way to end the session.
If you need any more explanation, or more of the code to understand, please let me know. Any help is much appreciated.
You can change session_destroy() by unset($_SESSION['name']) , like this:
if (isset($_POST['name'])) {
$_SESSION['name'] = $_POST["name"];
echo "<input type='submit' name='logout' method='logout' value='Log out'>";
echo "</form>";
echo "</h1>";
echo "</div>";
if(isset($_POST['logout']) && ($_POST['logout'] == "Log out")) {
unset($_SESSION['name']);
}
}
Use session_unset() to destroy all registered variables in the session and if you want unset a specific variable like name use unset($_SESSION['name'])
Do NOT unset the whole $_SESSION with unset($_SESSION) as this will disable the registering of session variables through the $_SESSION superglobal.
You can try to add a call to session_commit() just after session_destroy();
The code below handles the Logout hyperlink, so it creates the global variable logout and it unsets the session
Log out
<?php
if(isset($_GET['logout'])) {
session_unset();
}
?>
I am learning session handling while submitting form data between multiple php pages.
For simple example, I have a php form myform.php that asks the user to enter a password and directs to myaction.php. I want to start the session before the user enter the password. If he enters the password as 1234 within one minute, it displays "valid". If he enters the password after 1 minute, it should display "Session timeout".
Here is my code that doesn't work.
myfrom.php
<?php
session_start();
$_SESSION['start'] = time();
$_SESSION['expire'] = $_SESSION['start'] + (01 * 60);
?>
<html>
<form nmethod="post" action="myaction.php">
<input type="text" name="myvalue"/>
<input type="submit" value="SignIn" name="submit1">
</form>
</html>
myaction.php
<?php
if ($_POST['submit1'])
{
$v1 = "1234";
$v2 = $_POST['myvalue'];
$_SESSION['now'] = time();
if ($_SESSION['now'] > $_SESSION['expire'])
{
echo 'Session Timeout!';
header('refresh:05;Location: http://localhost/myform.php');
}
else if(($_SESSION['now'] <= $_SESSION['expire']) && ($v1==$v2))
echo 'Valid Password!';
else
echo 'Invalid password!';
}
?>
Where did I go wrong?
the first thing I think you have a typo
<form nmethod="post"
there is additional "n" before method, remove it .
the second thing you have to use start_session() whenever you want to use anything from session.
I'm working on a forum software right now, and I'm doing very well so far, it all works, but I can't get this to get away, I'm not really sure how.
I get Undefined index: username whenever I call
$_SESSION['username']
I start the session in a seperate login file, but get this error whenever calling it, I only get it when someone isn't logged in, so like if I'm logged in, it doesn't show it, only when I'm not logged in, how can I get rid of it?
Here's my whole code for the page:
<?php
session_start();
include_once("connect.php");
$find = "SELECT id,name,description FROM forums";
$run_find = mysql_query("$find");
if ($_SESSION['username']) {
print "Welcome, " . $_SESSION['username']. "!<br/>";
}
while($is = mysql_fetch_assoc ($run_find))
{
$id = $is['id'];
$name = $is['name'];
$des = $is['description'];
print "<div style='width:500px;background-color:#FFCCFF;'>";
print "Forum : <a href='topics.php?t=$id'>". $name . "</a><br/>" .$des . "<br/><hr>";
print "</div>";
}
if (!$_SESSION['username']) {
echo "
<form action='loginnext.php' method='post'>
Username: <input type='text' name='username'><br/>
Password: <input type='password' name='password'><br/>
<input type='submit' name='submit' value='Login'><br/></form>";
}
if ($_SESSION['username']) {
echo "<a href='logout.php'>Logout</a>";
}
?>
If the user is not logged in, the session variable will not be set, thus the error message.
Use if(isset($_SESSION['username'])) instead of if($_SESSION['username'])
if (!empty($_SESSION['username'])) is the construct you wanted. isset and empty can not throw notices.