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');
?>
Related
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
I have a form, this form is on a page called question1.php, and I want it to load question2.php when the submit button is pressed.
<form action="question2.php" method="post">
<input type="radio" name="ans" value="cuboid">
<input type="radio" name="ans" value="cone">
<input type="radio" name="ans" value="cylinder">
<input type="radio" name="ans" value="sphere">
<input type="submit" value="submit" name="submit">
</form>
But I also have this php code
<?php
if(isset($_POST['submit'])) {
if(isset( $_POST['ans'])) {
$selected_answer = $_POST['ans'];
if($selected_answer == "cuboid") {
$_SESSION["cuboid"] = ((int)$_SESSION["cuboid"]) + 1;
}
}
}
?>
EDIT: I have made a simpler demo to try and explain myself better, i have got three pages.
page1.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<form action="page2.php">
<input type="submit" value="submit" name="submit">
</form>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
?>
</body>
</html>
page2.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<form action="page3.php" method="post">
<input type="radio" name="ans" value="color">
<input type="submit" value="submit" name="submit">
</form>
<?php
// Echo session variables that were set on previous page
if(isset($_POST['submit'])) {
if(isset($_POST['ans'])) {
$selected_answer = $_POST['ans'];
if($selected_answer == "color") {
$_SESSION["favcolor"] = "red";
}
}
}
?>
</body>
</html>
And page3.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
echo "Favorite color is " . $_SESSION["favcolor"] . ".";
?>
</body>
</html>
So on the first page I declare the session variable "favcolor", then on the second page if the user selects the radio button I want to update the color to red, however it just won't chane for me, on the third page it is still printing green
You had issue on page2.php because you are submitting form on page3.php but you are handling submission request on page2.php. So i corrected below for you.
page1.php
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<body>
<form action="page2.php" method="post">
<input type="submit" value="submit" name="submit">
</form>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
?>
</body>
</html>
page2.php
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<body>
<form action="page3.php" method="post">
<input type="radio" name="ans" value="color">
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>
page3.php
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<body>
<?php
// Echo session variables that were set on previous page
if (isset($_POST['submit'])) {
if (isset($_POST['ans'])) {
$selected_answer = $_POST['ans'];
if ($selected_answer == "color") {
$_SESSION["favcolor"] = "red";
}
}
}
echo "Favorite color is " . $_SESSION["favcolor"] . ".";
?>
</body>
</html>
I hope this should work.
$_SESSION["cuboid"] = isset($_SESSION["cuboid"]) ? $_SESSION["cuboid"] : 0;
$_SESSION["cuboid"] = ((int)$_SESSION["cuboid"]) + 1;
From what I can tell, you are attempting to redirect to another webpage.
Try sending HTTP headers using the header() function.
header('Location: http://example.com/page3.php');
exit();
If the HTTP header doesn't work at first, turn on the output buffer using ob_start().
You are checking for $_POST['ans'] in the wrong page.
Currently you are checking for 'ans' when user enters page2.php, which is before the user submits the form that contains that input element.
When you submit the form on page2.php, the action sends the request over to page3.php, so you actually want to move your if statement that sets the color to green to the top (below session start) of page3.php
I included header.php to every page for header logo and check page permission by SESSION!
So if you request main.php without login (session null state), It will display login page and do exit in header.php for not showing current page content.
Its header.php is work for every page except login.php.Because login page is not show login form
So I want to show login form ,how can I check in header.php for its ? Sorry for my poor english :(
login.php
<?php
session_start();
include("header.php");
if($_POST){
//set session logged in
}
?>
<div class="login-box ">
<h3> Log In </h3>
<form method="POST">
<input type="text" name="user" placeholder="Type User Name"><br>
<input type="password" name="pass" placeholder="Type Password"><br>
<input type="submit" name="submit" value="Login" class="button">
</form>
<span id="signup_text">You are not still a member.Click Sign Up</span>
</div>
header.php
<?php
session_start();
?>
<div id="header" class="container">
<div id="logo">
<h1>Online Quiz Management</h1>
</div>
</div>
<?php
if (isset($_SESSION['login'])) {
echo "<div id='menu'><ul><li>HOme</li><li>Signout</li></ul></div>";
}
else {
echo "<div class=head1> Your are not logged in<br> Please <a href=login.php>Login</a><div>";
exit;
}
?>
</div>
</div>
?>
main.php
<?php
session_start();
include("header.php");
?>
//show main code if logged in
Just add a check if the current page is login.php and don't run your block if it is
else if(!basename($_SERVER['PHP_SELF']) == "login.php") {
echo "<div class=head1> Your are not logged in<br> Please <a href=login.php>Login</a><div>";
exit;
}
i think u dont need to include the header.php to login.php, since it is check for the session, while the login.php is the session starter...
after all, try to add this to the header.php :
session_start();
if(!isset($_SESSION['login'])){
echo '<script language="javascript">';
echo 'alert("Please Login")';
echo '</script>';
echo("<script>location.href = 'login.php';</script>");//direct the user to login.php if they aren't logged in
}
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;
}
?>
How to redirect after the submit button to the same page? Note: in form i have two button, I want show user only one link, when the user click on it he must be redirected to another link and this link will redirect him to do same page form but this time showing the button for submit.
I'm not familiar with PHP (session) but at least I have an idea how it will work. This is the idea:
<form method="get" action="index.php">
<br>
<h2> ID : <input name="id" placeholder="your Id"/ size="45"/></h2><br>
</br>
<?php if (isset($_SESSION['link'])) {
?>
<input type="submit" name="submit" value="==>Submite<==" id="submit" />
<?php
} else {
?>
Click Here Before Submit
<?php }
?>
</form>
<?php
if(isset($_GET['link'])){
session_start();
{$_SESSION['link'] = $_GET['link'];}
header('location: redirection.php');
}
?>
Page redirection.php should test in index.php if the user clicked on the link if yes so user will be auto redirected to index.php with button submit header("Location: index.php").
exemple website : old-skys.net/cc
<h2> ID : <input name="id" placeholder="your Id"/ size="45"/></h2><br>
</br>
<?php if ($_GET['action']=='step2') {
?>
<input type="submit" name="submit" value="==>Submite<==" id="submit" />
<?php
} else {
?>
Click Here Before Submit
<?php }
?>
Please test it