Session lost from php page to codeignighter project - php

I have a button that POST data to my site
<form action="https://www.mysite.co.uk/some/index.php" method="POST">
<input type="hidden" name="userid" value="ZUpmenVaN0ZVTTBmejNGZGNwZGFha1NmR0tuSjdaT3VYdjV5cTF4WGtISzRvK0ptOC9vZmQyc3J3T3cwTmplbWZ3alhod0xMYUhlQ2xLSng4WWI4ZEE9PQ2">
<input type="submit" value="Go" style="font-size:14px; padding:20px;">
</form>
I then turn it into usable session
mysite.co.uk/some/index.php
<?php session_start();
<?php
if (isset($_POST['userid'])) {
$_SESSION['userid'] = $_POST['userid'];
$userid = $_SESSION['userid'];
}
if (isset($_SESSION['userid'])) {
echo "You are logged in and have access to these tests.";?>
code here
<?php
} else {
header ('location: ../index.htm');
}
?>
this all works well.
I now want to use this within a countersign project with the same website (this is where it goes wrong)
mysite.co.uk/some/tests/ETray/New/
<?php
$this->load->library('session');
$this->session->set_userdata('user_id', $_POST['userid']);
$userID = $this->session->userdata('user_id');
echo "$userID";
?>
I get error
Message: Undefined index: userid
unless I post direct to this page like
<form action="https://www.mysite.co.uk/some/tests/ETray/New/" method="POST">
<input type="hidden" name="userid" value="ZUpmenVaN0ZVTTBmejNGZGNwZGFha1NmR0tuSjdaT3VYdjV5cTF4WGtISzRvK0ptOC9vZmQyc3J3T3cwTmplbWZ3alhod0xMYUhlQ2xLSng4WWI4ZEE9PQ2">
<input type="submit" value="Go" style="font-size:14px; padding:20px;">
</form>
Then no error and the echo prints out.
Question
how do I get this to echo out on this page with the first form not the second ?

Looks like that error Message: Undefined index: useridis due to the undefined index of $_POST['userid']
Another one, where does mysite.co.uk/some/tests/ETray/New/ points to?? Is that inside controller or where???

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

Why am I unable to store form input in a session?

I'm trying to save the input from my form in a session. I have this code in index.php:
<?php
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$_SESSION['str1'] = $_POST["streamer1"];
$_SESSION['str2'] = $_POST["streamer2"];
}
?>
<form method="post" action="view.php" id="steamerEingabe">
<label class="white-text font-weight-light">Streamer #1</label>
<center><input type="text" name="streamer1" class="form-control"></input></center><br>
<label class="white-text font-weight-light">Streamer #2</label>
<center><input type="text" name="streamer2" class="form-control"></input></center><br>
<button type="submit" class="btn btn-outline-cyan">Watch Now</button>
</form>
And now I want to work with this session in my view.php:
<?php
session_start();
echo $_SESSION['str1'];
echo $_SESSION['str2'];
session_destroy();
?>
But this does not work. I get these errors
Notice: Undefined index: str1 in /var/www/vhosts/default/htdocs/multistream/view.php on line 4
Notice: Undefined index: str2 in /var/www/vhosts/default/htdocs/multistream/view.php on line 5
How can I fix this?
It looks like the problem is this: action="view.php"
Your form is submitting directly to view.php, but the code that sets the session values is in the same file with the form.
Instead, you can submit the form to the same page (index.php), and redirect to view.php after setting the session values if the form is submitted.
<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$_SESSION['str1'] = $_POST["streamer1"];
$_SESSION['str2'] = $_POST["streamer2"];
header('Location: view.php'); // redirect after setting session values
}
?>
<!-- submit to self -->
<form method="post" action="" id="steamerEingabe">
<label class="white-text font-weight-light">Streamer #1</label>
<center><input type="text" name="streamer1" class="form-control"></input></center><br>
<label class="white-text font-weight-light">Streamer #2</label>
<center><input type="text" name="streamer2" class="form-control"></input></center><br>
<button type="submit" class="btn btn-outline-cyan">Watch Now</button>
</form>
If you make these changes, view.php should work as expected. But you should still check that those session values are set before using them. If you go to view.php directly without submitting the form in index.php, they probably won't be there.

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

Form Text to specify session name

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

PHP session variable not being set

I am aware that there are several topics about this but after hours of reading I still can't figure out what's going wrong.
I'm building a survey with a login screen at index.php. The user is required to insert their name and submit the form. The username should be saved and passed onto setup1.php.
This is part of my index.php:
<?php
session_start();
print session_id();
?>
<form id="login" method="POST" action="setup1.php">
<input id="participant" name="participant" type="text" size="20"/>
<input type="submit" name="start" value="Start"/>
</form>
<?php
$name = $_POST['participant'];
$_SESSION['username'] = $name;
?>
Start of setup1.php:
<?php
session_start();
print session_id();
print_r($_SESSION);
echo $_SESSION['username'];
?>
My $_SESSION variable is empty, I have nothing printed on the following page setup.php. I would appreciate if you could help.
Your $_POST code is in the wrong file. Your form is going to setup1.php, but you're trying to set the $_SESSION in your index.php.
You need to take it out of there and put it in setup1.php:
<?php
session_start();
if (!isset($_POST['participant'])) {
die('No $_POST data');
}
$_SESSION['username'] = $_POST['participant'];
print session_id();
print_r($_SESSION);
echo $_SESSION['username'];
?>
Also, make sure that you're using $_SESSION and not %_SESSION. I hope it was just a typo.
Your form hasn't been submitted when you set the $_SESSION['username'], i.e., $_POST['participant'] has no value.
You should move the piece of code below from index.php to setup1.php
<?php
$name = $_POST['participant'];
$_SESSION['username'] = $name;
?>
index.php
<?php
session_start();
?>
<form id="login" method="POST" action="setup1.php">
<input id="participant" name="participant" type="text" size="20"/>
<input type="submit" name="start" value="Start"/>
</form>
setup1.php
<?php
session_start();
if(isset($_POST['participant']) && ! empty($_POST['participant']))
{
$_SESSION['username'] = $_POST['participant'];
echo $_SESSION['username'];
}
?>`

Categories