Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
This is my page
I would make possible that if somebody writes in that imput form a word, PHP redirects him to a page. Is it possible? Can you help me? Thank you alot.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/index.css">
<title>Magico mondo di Rob</title>
</head>
<body background="img/bg.png">
<div id="paroladiv">
<form>
<center>
<p1> Benvenuto! </p1>
</center>
<center>
<p2 id="parolatext">Inserisci la tua parola magica.</p2>
</center>
<p> </p>
<center>
<form action="" method="POST">
<input id="parola" type="text" name="parola"
placeholder="Parola va qua."><br>
</form>
</center>
</body>
</html>
<?php
if ($_POST['parola'] == google) {
header("Location: http://google.com");
}
?>
You have two things wrong .. Your HEADER needs to be located at the top of the file to prevent "Headers already sent" error. -- Plus it's always a good idea to throw a DIE in afterward for good measure.
Second, you need to put your POST query in quotes as it is a string you are looking for.
Plus as Fred mentioned in comments, you have a stray FORM tag
<?php
if ( !empty($_POST['parola']) && $_POST['parola'] == 'google') {
header("Location: http://google.com");
die();
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/index.css">
<title>Magico mondo di Rob</title>
</head>
<body background="img/bg.png">
<div id="paroladiv">
<center>
<p1> Benvenuto! </p1>
</center>
<center>
<p2 id="parolatext">Inserisci la tua parola magica.</p2>
</center>
<p> </p>
<center>
<form action="" method="POST">
<input id="parola" type="text" name="parola" placeholder="Parola va qua."><br>
</form>
</center>
</body>
</html>
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have created two pages , one is index.php and admin.php . In the first page or index.php , i have created a loggin form so that i can access the admin.php page trough the loggin or like this enter image description here
Now by logging in i go to admin.php page. Here is the question what i want to ask about that now when ever i click the back button or next button in the chrome. I am returning to the admin.php page . I have tryed the session_start() and the if(!isset($password) || !isset($user)){}. But this code for obvious reasons doesnt work . So can someone help me out with this ?
The code for the example is here index.php
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Admin</title>
</head>
<body>
Reset
<div class="image">
<img src="img/adi.png" alt="image"> <!-- image -->
</div>
<form action="inc/login.php" method="POST"><br>
<p class="title">Log In</p>
<label for="Username">User :</label>
<input type="text" name="username" id="user"> <br> <!--username -->
<label for="Password">Password :</label>
<input type="password" name="password" id="password" ><br><br> <!-- password -->
<label for="showpassword" class="showpassword">Show Password</label>
<input type="checkbox" name="checkbox" id="chk" ><br><br> <!-- checkbox -->
<input type="submit" name="submit" value="Log in" > <!-- enter -->
</form>
<?php
if(!isset($_GET['Login'])){
exit();
}else{
$check=$_GET['Login'];
if($check=="userEmpty"){
echo "<p class='class_login'>user is empty</p> ";
}elseif($check=="passwordEmpty"){
echo "<p class='class_login'>password is empty</p> ";
}elseif($check=="wrongUser"){
echo "<p class='class_login'>user is wrong</p> ";
}elseif($check=="Password"){
echo "<p class='class_login'>password is wrong</p> ";
};
} ;
?>
<script src="js/main.js"></script>
</body>
</html>
and the code for the admin.php is this one :
<?php
session_start();
if(!isset($username) || !isset($password)){
header("location:index.php?data=closed");
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<script>
window.history.forward();
</script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<div class="top">
Log Out
</div>
</body>
</html>
Besides using of session_start you need to store something in the session, using something like this:
$_SESSION['userId'] = <value>;
which must be set in your login.php and checked by your admin.php if user has access rigths to visit secured page. I.e. you need to check:
if (isset($_SESSION['userId']) && $_SESSION['userId'] == <value>) {
// access granted
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
<?php
session_start();
?>
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="pageContainer">
<form action="second.php" class="formLayout" method="post">
<div class="formGroup">
<label>Name:</label>
<input type="text" name="first_name"><br>
<input type="hidden" name="postback" value="true"><br>
</div>
<div class="formGroup">
<label> Car model:</label>
<div class="formElements">
<input type="radio" name="model" value="Mustang">Ford Mustang<br>
<input type="radio" name="model" value="Subaru">Subaru WRX STI<br>
<input type="radio" name="model" value="Corvette">Corvette<br>
</div>
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit'])) {
$_SESSION["first_name"] = $_POST["first_name"];
$_SESSION["model"] = $_POST["model"];
}
?>
</div>
</body>
</html>
I set up my code to set the value of the "first_name" and "model" names into my session variables.
When I try to access the values of the stored variables in the submission page of the form:
<?php
session_start();
?>
<html>
<body>
<?php
echo $_SESSION["first_name"];
echo $_SESSION["model"];
?>
</body>
</html>
I only receive the out of the model value, not the first_name value. I don't understand what I'm doing wrong here.
Let's assume your first file is called first.php. As shown in your <form>, the second one is second.php.
It seems that you are writing the data into the session in the first.php file, but this code will never run because you are submitting your form to second.php and not first.php!
So, move the code that writes the session variables into second.php instead. To test that it really worked, you can create a third.php to display them.
(I'm not sure why see the model set, but I guess it's still there from a previous test you did.)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am having trouble with session variables. I created the session variable on page1.php and then tried to display it on page2.php, but it didn't work. I made sure to add session_start(); at the beginning of the page so that's not the problem.
Here is my code for page1.php
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>website</title>
<link rel="icon" href="icon.png">
<link rel="stylesheet" type="text/css" href="style.css">
<?php
if(!isset($_POST["submit"])) {
$_SESSION["username"] = $_POST["username"];
}
?>
</head>
<body id="body">
<form action="page2.php" method="post">
<input type="username" id="username" name="username" placeholder="Username">
<input type="submit" id="submit" name="submit" value="Submit">
</form>
</body>
</html>
Here is my code for page2.php
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>website</title>
<link rel="icon" href="icon.png">
<link rel="stylsheet" type="text/css" href="style.css">
</head>
<body id="body">
Your username is: <?php echo $_SESSION["username"]; ?>
</body>
</html>
$_POST['username'] will not be available in page1.php it will only be available in page2 as that is the page that responds to the FORM being submitted.
Similiarly $_POST["submit"] will not be available in page1
Add this while testing to the top of your script, then even if you are developing on a site configured for a LIVE environment you will see errors like
Undefined index which should be being sent from page1.php
<?php
ini_set('display_errors', 1);
ini_set('log_errors',1);
error_reporting(E_ALL);
in page1.php you have
// v------ wrong logic?
if(!isset($_POST["submit"])) {
$_SESSION["username"] = $_POST["username"];
}
and then
<!-- v---------- page2.php -->
<form action="page2.php" method="post">
<input type="username" id="username" name="username" placeholder="Username">
<input type="submit" id="submit" name="submit" value="Submit">
</form>
That means, when you are submitting the form, you'll be redirected to page2.php, that doesn't handle the form datas $_POST.
You might want to place this piece of code in page2.php :
if(isset($_POST["submit"])) {
$_SESSION["username"] = $_POST["username"];
}
Note :
Top of your pages, you have :
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en-US">
As you can notice, there will be some characters (a vertical whitespace) rendered before the <!DOCTYPE html> that will activate the quirks mode on some browsers. Make sure the doctype is the first line in your page.
You have several things wrong
1. If statement
The code will only excecute IF $_POST['submit'] isn't initialized.
To fix this, you need to remove your '!' before is isset();
if(isset($_POST["submit"])) {
$_SESSION["username"] = $_POST["username"];
}
2. Your POST variable goes to page2.php
You need to add this to page2.php:
if(isset($_POST["submit"])) {
$_SESSION["username"] = $_POST["username"];
}
You just had the code in the wrong place. So close.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Like the title says, I'm having a problem regarding my validation php. It's just a simple login php, where if the user inputs the wrong information, a validation form will execute saying that it will return to the previous login page after the five seconds countdown. But, if the user inputs the right username and password, it will just display a welcome text on the validation php. However, whenever I run the program it executes both the welcome text AND the countdown, regardless of whether the inputted information is right or wrong.
Here's my first php:
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style>
body {
margin:0;
font-family: Calibri}
.style1 {
color: #FFFFFF}
</style>
</head>
<body>
<h1>Getting Input from USER</h1><h2>EXAMPLE 1</h2>
<form method="post" action="validatea.php">
<table width="200" border="0" cellspacing="0" cellpadding="5">
<tr>
<th bgcolor="#006699" scope="row"><span class="style1">Username</span></th>
<td><label>
<input type="text" name="txtUsername" id="txtUsername" required="required"/>
</label></td>
</tr>
<tr>
<th bgcolor="#006699" scope="row"><span class="style1">Password</span></th>
<td><label>
<input type="password" name="txtPassword" id="txtPassword" required="required"/>
</label></td>
</tr>
<tr>
<th colspan="2" scope="row"><div align="right">
<input type="reset" value="Clear"/>
<input type="submit" value="Login"/>
</div></th>
</tr>
</table>
</form>
<hr/>
</body>
</html>
Here's the Validation php:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script>
var ctr=5;
function countdown(){
window.document.getElementById("cnt").innerHTML=ctr;
ctr--;
if (ctr<0) window.location="samplea.php";
setTimeout("countdown()", 1000);
}
</script>
</head>
<body>
<?php
if(isset($_POST["txtUsername"])){
$uname="user";$psw="hello";
if($uname==$_POST["txtUsername"] && $psw==$_POST["txtPassword"]){
echo "<h2>Welcome " .$_POST["txtUsername"]."!</h2>";
else{
echo"Username and Password is invalid.<br/><br/>";
echo'This will redirect to login form in <label id="cnt"></label>seconds...';
echo'<script> countdown();</script>';
}
}
}
?>
</body>
</html>
You missplaced the } in the second IF
<?php
if(isset($_POST["txtUsername"])){
$uname="user";$psw="hello";
if($uname==$_POST["txtUsername"] && $psw==$_POST["txtPassword"]){
echo "<h2>Welcome " .$_POST["txtUsername"]."!</h2>";
}else{ //Forgot to close if }
echo"Username and Password is invalid.<br/><br/>";
echo'This will redirect to login form in <label id="cnt"></label>seconds...';
echo'<script> countdown();</script>';
}
}
?>
You haven't closed your second if in Validation php!
Close it and it will work fine
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I've got a problem to register variable session. I've ordered my code with MVC (View=Vue in french) modele. Like this :
My problem : Index.php include all of others files, so I put the 'session_start();' here. (Anyway I can't put it on other place without taking error log "session has already started")
Firstly index.php 'open' connexion.php, with connexion form for user. When he submit it, my controleur look if it's good, and redirect user to the Accueil.
I would like to register variable 'user' on my controler, but this doesn't work anymore.
My code :
Index.php
<?php session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Language" content="fr" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<link rel="stylesheet" href="Style/style.css" type="text/css" />
<title>Saisie des temps</title>
</head>
<body>
<div id=page>
<?php
include_once 'Vues/Header.php';
?>
<div id=corps>
<?php
include_once 'Vues/Menu.php';
if (!empty($_GET['page']) &&
is_file('Controleurs/'.$_GET['page'].'.php')){
include_once (dirname(__FILE__).'../../SaisieTemps/Controleurs/'.$_GET['page'].'.php');
}
else{
include_once (dirname(__FILE__).'../../SaisieTemps/Controleurs/GestionConnexion/connexion.php');
}
?>
</div>
<?php
include_once 'Vues/Footer.php';
?>
</div>
</body>
</html>
Controler : COnnexion.php
<?php $_SESSION['test']='test'; ==> WORKS BUT TO EARLY ^^
//Inclusion modele
include(dirname(__FILE__).'/../../Modeles/GestionConnexion/Connexion.php');
$login = isset($_POST['username'])?$_POST['username']:'';
$password = isset($_POST['password'])?$_POST['password']:'';
if(isset($_POST['submit'])){
$intError = verifConnexion($login, $password);
switch ($intError){
case 0:
//Connexion OK
$_SESSION['login']=$login; ==> DOESN'T WORK
?><script type="text/javascript">
document.location.href = '../../../SaisieTemps/index.php?page=GestionConnexion/Accueil';
</script><?php
break;
case 1:
//Pb mdp
?><script type="text/javascript">
document.location.href = '../../../SaisieTemps/index.php?page=GestionConnexion/Connexion.php&Error=1';
</script><?php
break;
case 2:
//Pb login
?><script type="text/javascript">
document.location.href = '../../../SaisieTemps/index.php?page=GestionConnexion/Connexion.php&Error=2';
</script><?php
break;
default:
?><script type="text/javascript">
document.location.href = '../../../SaisieTemps/index.php?page=GestionConnexion/Connexion.php&Error=3';
</script><?php
break;
}
}
else{
//Inclusion vue
include(dirname(__FILE__).'/../../Vues/GestionConnexion/Connexion.php');
}
View:Connexion.php
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<div id=contenu>
<form action="../../SaisieTemps/Controleurs/GestionConnexion/Connexion.php" method="POST" id=login>
<h1>Connexion</h1>
<fieldset id="inputs">
<label for="username">Identifiant : </label>
<input id="username" name="username" type="text"
placeholder="Identifiant" autofocus required/><br/>
<label for="password">Mot de passe : </label>
<input id="password" name="password" type="password"
placeholder="Mot de passe"required/>
</fieldset>
<fieldset id="actions">
<input type="submit" id="submit" value="Connexion" name="submit">
</fieldset>
</form>
<label class="errorLabel"><?php
if(isset($_GET['Error'])){
switch ($_GET['Error']){
case 1:
echo'Mauvais mot de passe';
break;
case 2:
print'Le login n\'existe pas';
break;
}
}?>
</label>
</div>
Thank's for your help, and sorry for my bad english
In your login form action is direct to PHP file (to Controller) not via index.php
And in Controler : COnnexion.php session not started.
Change logic of your application or add session_start() to this controller
Remove the whitespace before you php code. It is not allowed to put input before the session_start(); a space or a tab is also seen as content.