Form Text to specify session name - php

I want to allow the user to input some text, then use that text as their session username and then echo a welcome "username" at the top of each page, and then a logout button which will end the session and allow for the user to input a new username
Currently the code will allow the user to input a name, and then a Welcome will appear for that name, but it will be destroyed if the user changes pages, and the textfield to input a name never disappears either - This is the master page in codeigniter
<form action="" method="POST">
<input type="text" name="input_value">
<input type="submit" name ='in' value="LogIn">
</form>
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
$_SESSION['userses'] = '';
}
if(session_status() == PHP_SESSION_ACTIVE && $_SESSION['userses']=='' ){
$_SESSION['userses'] = $_POST['input_value'];
echo "Welcome, ".$_SESSION['userses']."!";
}
else{
echo "Welcome, ".$_SESSION['userses']."!";
}
?>
<form action="<?php session_destroy(); ?>" method="POST">
<input type="submit" name='out' value="LogOff">
</form>

You have to change
if(session_status() == PHP_SESSION_ACTIVE && $_SESSION['userses']=='' )
to
if(isset($_POST['in']) ){
$_SESSION['userses'] = $_POST['input_value'];
}
so when he submits username, this if will catch the post request and then add input to your username
you don't need to check session_status
edit:
and as the comments said, put session_start(); at the beginning of every page
example of code :
<?php session_start();
if(isset($_SESSION['userses'])) echo "Welcome".$_SESSION['userses'];
?>
<form action="" method="POST">
<input type="text" name="input_value">
<input type="submit" name ='in' value="LogIn">
</form>
<?php
if(isset($_POST['in']) ){
$_SESSION['userses'] = $_POST['input_value'];
}
?>
<form action="<?php session_destroy(); ?>" method="POST">
<input type="submit" name='out' value="LogOff">
</form>
You are using codeigniter, why not use session library :
https://www.codeigniter.com/user_guide/libraries/sessions.html

Related

Storing form data in session with php

I'm testing a really basic PHP form, where the form data is saved in a session.
Later, i want that session data to be the default value of the form:
<strong>Test Form</strong>
<form action="" method"post">
<input type="text" name="var" value=<?php $name ?>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
// starting the session
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['var'] = $_POST['var'];
$name = $_SESSION['var'];
}
echo $name;
?>
So, for example if i input "MyName" it should echo "MyName" and in the form there should be the value "MyName". The problem with the actual code is that it gives an E_NOTICE : type 8 -- Undefined variable: name -- at line 18 error. I think that the variable is not being stored, can someone help me out on this?
The first error I notice is this piece of code:
<form action="" method"post">
Where method does not contain the symbol "=" which causes the loss of the parameter post.
Furthermore, the "session_start ()" function must be placed before any other code. The code derives from this is as follows:
<?php
// starting the session
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['var'] = $_POST['var'];
$name = $_SESSION['var'];
} else {
$name = null;
}
?>
<strong>Test Form</strong>
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="var" value="<?= ($name != null) ? $name : ''; ?>">
<input type="submit" name="Submit" value="Submit!" />
</form>
Spotted a few things:
I'd put the session_start(); at the top of the page before outputting anything.
Your method was incorrectly written it needs a '=' when specifying the method - thats the main reason why nothing was being stored, the form wasn't submitting properly.
Same with how you've put in the value on the name input - it has no '=' and you don't close the input tag properly - I've left it blank and added a placeholder - you can change it to what you need.
Heres how I'd do it:
<?php session_start(); ?>
<strong>Test Form</strong>
<form action="" method="post">
<input type="text" name="var" value="" placeholder="enter name">
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
if (isset($_POST['Submit'])) {
$_SESSION['var'] = $_POST['var'];
}
// Store the session in a variable after the submit - otherwise it will be forgotten on refresh
$name = $_SESSION['var'];
// check if session exists
if(isset($name)) {
echo $name;
}
else {
echo 'no name entered...';
}
?>
You can edit the above to hide the form if a name has been submitted etc. Use session_destroy(); to reset the stored session.
Cheers

restrict access to the second form page

Helloo, I have a mutistep form functionality where
in "Step1" ( page 1 ) i have a form with three radioButton
And in step 2 (page2 ) I have another form.
So, My problem is that I want to restrict access to page2 via URL like if I type in Browser Addressbar : localhost/page1.php then it should load page1.php but when I write localhost/page2.php then it should restrict user to have access for page2 and I would like to be redirected to localhost/page1.php
so i tried this :
page1 :
<form class="type" action="page2.php" method="post">
<label style="text-align:center;"> Type de compte : </label>
</br> </br>
<input type="radio" name="typeCompte" value="enseig" required>
Enseignant </br>
<input type="radio" name="typeCompte" value="etud" required>
Etudiant </br>
<input type="radio" name="typeCompte" value="admin" required>
Adminstrateur </br>
</br></br>
<input type="submit" class="make" name="submit" value="Valider">
</form>
<?php
if(isset($_POST['submit']))
{
$_SESSION['log']=1;
}
?>
page 2 :
<?php
session_start();
if($_SESSION['log']!=1)
header("location: page1.php");//redirecting to first page
?>
Try putting session variable as bool like...
In Page1.php
<?php
session_start();
if($_POST['submit']){
$_SESSION['log'] = "true";
header("Location: page2.php");
}
?>
Then In Page2.php
if(!isset($_SESSION['log']) && $_SESSION['log'] == "false"){
//send them back
header("Location: page1.php");
}
else{
//your logic if any.
}
I think in Php session might be working on true false basis only..
to check for session you can use isset command
<?php session_start();
//if ! means not isset check if the session is active
if(!isset($_SESSION['log']))
header("location:page1.php");
?>
in your page where you assign the session for log you can use it to assign any value for the user that you might need later. however do some reading about .htaccess and put your limited access file in a different dir
on page 1
$t='1';
$_SESSION['log']=$t;
and very very important on the first line after <?php on page 1 you must have
session_start();
edit try this that i wrote only spend 5 min so it is not much but i tested it and it works make 2 file test1.php and test2.php
test1.php
<?php
session_start();
if(isset($_POST['set'])){
$log='1';
$_session['log']=$log;
include_once("test2.php");
exit;
}
if(isset($_POST['dontset'])){
include_once("test2.php");
}
?>
<html>
<body>
<form action="" method="post">
<input type="submit" name="set" value="set">
<input type="submit" name="dontset" value="dontset">
</form>
</body>
</html>
test2.php
<?php
session_start();
if (!isset($_session['log'])){
echo 'you are not authorised';
header("location:test1.php");
}
if (isset($_session['log'])){
echo 'you are authorised';
}
?>
<html>
<body>
<form action="" method="post">
<input type="submit" name="destroy" value="destroy">
</form>
</body>
</html>
<?php
if(isset($_POST['destroy'])){
session_destroy();
include_once("test2.php");
exit;
}
?>
it also shows you how to logout the user using sessions

PHP - access different pages

I want to make my php page like the following scenarios:
If I click the "my account", it will link me to login page if I'm
not logged into the site.
If I click the "my account" and I'm
already logged to the site, it will take me to the user account.
You can PHP Sessions for this purpose.Have a look at the below example
Login With Session
You must set a session variable
Login Page: login.php
e.g.
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
if(($_POST['username']=="myusername") && ($_POST['password']=="password"))
{
$_SESSION["mysession"] = $_POST['username'];
}
else {
?>
<form action="" method="post">
User Name:<br>
<input type="text" name="firstname" value="">
<br>
Password:<br>
<input type="password" name="password" value="">
<br><br>
<input type="submit" value="Submit">
</form>
<?php }
?>
My Account Page:
<?php
// Start the session
session_start();
if (isset($_SESSION['mysession'])){
echo $_SESSION['mysession'];
}
else
{
header("Location: http://example.com/login.php");
die();
}
?>

Login Page Username and Password cannot be posted (PHP)

Username and password not appear on Page 2.PHP although I post it to Page2.PHP
Page1.PHP
<form name="form1" method="post" action="Page2.php">
<input type="text" name="txtLogin">
<input type="password" name="txtPWD">
<input type="submit" name="btnSub" value="go">
</form>
Page2.PHP
<?php
if(isset($_REQUEST['txtLogin']))
{
session_start();
$_SESSION['login']=$login;
}
if(isset($_SESSION['login']))
header('Location: detail.php');
else
header('Location: index.html');
?>
put this on page2.php
if(isset($_POST['txtLogin']) && isset($_POST['txtPWD']))
{
//get values & do other scripts like saving values on sessions
$user = $_POST['txtLogin'];
$pass = $_POST['txtPWD'];
echo $user.'<br>'.$pass;
}
else
{
//event here
}
The problem is here:
$_SESSION['login']=$login;
You are using the $login variable, but it isn't actually being set anywhere.
A few lines further up, we see that the login name is actually in $_REQUEST['txtLogin'], not $login. So you should be using that.
$_SESSION['login']=$_REQUEST['txtLogin'];
Hope that helps.
Check settings: enable_post_data_reading, request_order, variables_order, gpc_order on http://www.php.net/manual/en/ini.core.php

How to redirect with a $_SESSION variable from a form

I'm a bit inexperienced so go easy on me.
I need to save the value from a form textarea before the form is submitted (I need it even after the page is reloaded).
After the reload, I need to redirect to a predefined page on the site that includes the textarea value on the very end on the URL.
I have something like this so far:
<php?
session_start();
$_SESSION['textarea_value'] = $_POST['textarea_name'];
?>
// below is called directly after a popup form submission
location.reload();
if ($_SESSION['textarea_value'] != null) {
header("Location: http://www.xxxxxxxx.com/?s=$_SESSION['textarea_value']");
unset($_SESSION['textarea_value']);
}
Okay, so here's what I would do:
Method #1
index.php
<?php
if($_POST)
{
session_start();
$_SESSION["someVar"] = $_POST["someVar"];
header("Location:otherPage.php");
}
?>
<form action="" method="post">
<textarea name="someVar"></textarea><br/>
<input type="submit" value="submit">
</form>
otherPage.php
<?php
session_start();
$someVar = $_SESSION["someVar"];
?>
Method #2: If you want to do it with a get request, you don't even need to use sessions:
index.php
<?php
if($_POST)
{
header("Location:otherPage.php?someVar=".$_POST["someVar"]);
}
?>
<form action="" method="post">
<textarea name="someVar"></textarea><br/>
<input type="submit" value="submit">
</form>
otherPage.php
<?php
$someVar = $_GET["someVar"];
?>
Method #3: You could also even take out the redirection entirely:
index.php
<form action="otherPage.php" method="post">
<textarea name="someVar"></textarea><br/>
<input type="submit" value="submit">
</form>
otherPage.php
<?php
$someVar = $_POST["someVar"];
?>

Categories