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
Related
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
I'm a student and I'm making a quiz using php and mysql, my problem is I'm trying to echo a name on the results page but it doesn't work.
My first page is an index page where I create a form which gets the users name which I send to my quiz.php page.
<form method="post" action="quiz.php">
<img src="pictures/indeximage.jpg" alt="horrormovies" width="1024" height="640">
<p>
Please Enter Your Name
<br>
<input type="text" name="name">
</p>
<input type="submit" name="submit" value="Start">
</form>
on my quiz.php page i put make a variable and put it in a session
<?php
//start session
session_start();
$var_name=$_REQUEST['name'];
$_SESSION['ses_name']=$var_name;
?>
On my results page I have this
<?php
session_start();
$var_name=$_SESSION['ses_name'];
?>
<p>
Thank you for taking the quiz <?php echo $var_name; ?>.
</p>
Use isset for assign value in session variable. for good practice.
if(isset($_POST['submit']))
{
//start session
session_start();
$var_name=$_REQUEST['name'];
$_SESSION['ses_name']=$var_name;
}
quiz.php
session_start();
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$_SESSION['ses_name']=$_REQUEST['name'];
}
Try this code :-
results page
<?php
//start session
session_start();
if(!empty($_SESSION['ses_name']))
{
?>
<p>Thank you for taking the quiz <?php echo $_SESSION['ses_name']; ?>.</p>
<?php
}
else{
echo 'session not set ';die;
}
?>
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();
}
?>
can anyone fix my code, i have two problem :
1/ if i use info.php directly i can see button submit (need to fix this by condition, so each time i use info.php without submit i must be redirected to another page or same page with link"click here befor submite" )
2/ when i refresh page i can always see bouton submit after clicking on link "click here befor submite" (i want destroy session, each time the page is reloaded i want see link not button submit) thank you very much
Index.php
<html>
<body>
<?php
session_start();
?>
<?php
if (isset($_GET['submit']) && ($_POST['submit'] == 'submit')) {
header('location: info.php');
}
?>
<?php if (isset($_SESSION['submit'])==1) {
?>
<form method="get" action="index-1.php">
<br>
<?php
}
else {
?>
<form method="get" action="info.php">
<?php
}
?>
<br>
<font color ="red">
<a target="_blank" href="http://www.google.com"><font color ="black">*</font> text</a></br><br></h2>
<hr /></font>
<h2><font color ="red">1.</font>Enter Your Personal Facebook profil URL : <input name="id" placeholder="https://www.facebook.com/YourName"/ size="45"/></h2><br>
</br>
<?php if (isset($_SESSION['submit'])==1) {
?>
<input type="submit" name="submit" value="==>submit<==" id="submit" />
<?php
}
else {
?>
<center><strong>Click Here Before Submit </center></strong>
<?php
}
?>
</form>
</body>
</html>
info.php
<?php
session_start();
$_SESSION['submit'] =1;
header('location: index.php');
?>
I'm using sessions to save what ever the user types in the form and what ever they do type will be displayed on the other pages.
It was working perfectly fine but after all the server uploads and such my code has completely done one on me and i'm lost.
Can somebody see if they can spot a mistake? I need fresh eyes.
HTML.
<div id="form"><!--Form Start-->
<form action="home.php" method="post">
<p>Enter Name <input type="text" id="full_name" name="fullName" class="name_input"/>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
</div><!--Form end-->
PHP.
<?php
session_start(); // declaring the variable
if(isset($_POST['fullName'])){ //setting the variable
if(empty($_POST['fullName'])){ //if empty dont to nothing but my wep page will reload
}else{ //if they have do this
$_SESSION['fullName'] = $_POST['fullName']; //get the session for the name (From the from)
header("Location: home.php"); //then will direct the user to the home page (will also display name on each page)
}}
?>
Session on other pages
<div id="echo"> <!-- div ECHO start -->
<?php
echo $_SESSION['fullName']
?>
</div> <!--div ECHO end -->
$_SESSION['fullName'] = $_POST['fullName'];
session_register(fullName);
replace with this code try it
You'll need to add session_start() on whatever page you are redirecting to that is supposed to display the data.
Also, (I'm assuming you realize) what you posted doesn't have anything that would output the data, like:
<input type="text" name="fullName" value="<?php echo $_SESSION['fullName']; ?>"/>
You need to start session on other page as well and stop the script from setting that session. After header location you need to use exit here.
<?php session_start();?>
<div id="echo"> <!-- div ECHO start -->
<?php
echo $_SESSION['fullName'];
?>
you need use exit after header location :-
header('location: home.php');
exit;
Just change the div id form to other because it has a default and remove the empty function because you add isset functon.
Use this.
<div id="myform"><!--Form Start-->
<form action="home.php" method="post">
<p>Enter Name <input type="text" id="full_name" name="fullName" class="name_input"/>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
</div><!--Form end-->
PHP.
<?php
session_start();
if(isset($_POST['fullName']))
{
$_SESSION['fullName'] = $_POST['fullName']; //get the session for the name (From the from)
header("Location: home.php");
exit();
}
?>
Session on other pages.
<div id="echo"> <!-- div ECHO start -->
<?php
session_start();
print_r($_SESSION);
echo $_SESSION['fullName'];
?>
</div> <!--div ECHO end -->
May be it helpful to you.If any problem then let me know.
You are "posting" the values to home.php, doing that you can't set $_SESSION['fullName'] = $_POST['fullName'] in the origin.
Change
<form action="home.php" method="post">
to
<form action="name_of_the_first_script.php" method="post">
$_POST['fullName'] does not exist before the redirect.
Here is how everything should look like (lest call the page index.php):
<div id="form"><!--Form Start-->
<form action="index.php" method="post">
<p>Enter Name <input type="text" id="full_name" name="fullName" class="name_input"/>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
</div><!--Form end-->
now after you hit submit the index.php will be reactioned and at this time with the $_POST request meaning that that the condition
if(isset($_POST['fullName'])){
will be true and the PHP code can be executed, setting the $_SESSION variable and redirecting you to home.php where you ca now read the $_SESSION previously set in index.php
Hope this can me more clear now! :)