I have log-in page, now I want to use the username input for two separate PHPs. how can I do it?
<form method = "post" action="login.php">
<input type = "hidden" name = "submitted" value = "true"/>
<h1>НЭВТРЭХ</h1>
<div>
<input type="text" placeholder="Student ID" required="" id="username" name="username"/>
</div>
<div>
<input type="password" placeholder="Password" required="" id="password" name="password"/>
</div>
<div>
<input id="button" type="submit" name="submit" value="НЭВТРЭХ" />
Lost your password?
</div>
</form><!-- form -->
The login.php is the first php calling the string. this php gets the string and works fine. but i want to use this string for another PHP.
In your login.php file you do something like this:
<?php
// Start the session engine for this php file
session_start();
// Might want to add more validation to those variables before using them
if(isset($_POST['username']) && isset($_POST['password']))
{
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
}
?>
and then you can access those $_SESSION variables from the other php file.
See W3School for more info on sessions.
In login.php store all data to session and use
header('Location: http://www.example.com/');
in second script unset those values of session
Related
There are similar questions related to the topic but none of them have solved my problem. Its kind of weird but my $_SESSION is working on the same page but not on any other page. If I put isset($_POST['submit') the condition doesn't satisfy and without it the $_SESSION remains null.
This is my code.
This is the login page.
<!-- Login.php -->
<?php
session_start();
?>
<html>
<body>
<form method="post" action="profile.php">
<fieldset>
<legend>
Login
</legend>
<label> User ID :</label> <input type="text" placeholder="Username" name="user"><br>
<label> Password :</label> <input type="password" placeholder="Password" name="password">
<input type="submit" name="submit" value="Login">
</fieldset>
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
$_SESSION['USER']= $_POST['user'];
$_SESSION['PASS']=$_POST['password'];
}
?>
This is where I want my session variable to appear.
<!-- profile.php -->
<?php
session_start();
echo "Session user is ".$_SESSION['USER']."<br>";
unset($_SESSION['USER']);
unset($_SESSION['PASS']);
session_unset();
session_destroy();
?>
This is what I have tried :
Changing form method to GET.
Using $_REQUEST and $_GET.
Using $_SESSION on the same page. It works on the same page.
Checking session id. The session on the other pages are present but values are either null or empty.
Running the code without isset(). In that case all the session variables remain NULL.
$_POST['submit'] and the rest of the post parameters are not available in Login.php
They are available only in profile.php because your form action points to it.
You may move the following code after the session_start() in profile.php.
if(isset($_POST['submit'])){
$_SESSION['USER']= $_POST['user'];
$_SESSION['PASS']=$_POST['password'];
}
Keep in mind that you unset the session values in the end of profile.php
I am trying to create php multipage forms, and I use PHP sessions for this purpose.
However, when there is an error in user input and I want the form to ask user to fill in the form again with correct inputs, the forms field will not hold the data that the user has already put in so the user has to start things all over again.
How to make forms sticky with php session?
Thanks
My code is as bellow
<?php
// Session starts here.
if (!isset($_SESSION)) session_start();
?>
<form action="registration.php" method="post">
<center><h8>Please create your user name and password</h8></center>
<div class="imgcontainer">
<img src="phone.gif" alt="Welcome" class="avatar">
</div>
<div class="container">
<label><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="username" required value="<?php if(isset($_POST['username'])) echo $_POST['username'];?>">
<label><b>Password</b></label>
<input type="Password" placeholder="Enter Password" name="password" required>
<label><b>Confirm Password</b></label>
<input type="Password" placeholder="Confirm Password" name="confirm" required>
<span id="error" width=100%>
<!---- Initializing Session for errors --->
<?php
if (!empty($_SESSION['error'])) {
echo "<error>".$_SESSION['error']."</error>";
unset($_SESSION['error']);
}
if (isset($_POST['username'])){
$_SESSION['username'] = $_POST['username'];
echo $_SESSION['username'];
echo $_POST['username'];
}
?>
</span>
<br>
<input type="reset" value="Reset" />
<input type="submit" value="Next" />
</div>
and the registration php contains
<?php
if (!isset($_SESSION)) session_start();
// Checking first page values for empty,If it finds any blank field then redirected to first page.
if (isset($_POST['username']))
{
if (($_POST['password']) === ($_POST['confirm']))
{
foreach ($_POST as $key => $value)
{
$_SESSION['post'][$key] = $value;
}
}
else
{
$_SESSION['error'] = "Password does not match with Confirm Password.";
if (isset($_POST['username'])){
$_SESSION['username'] = $_POST['username'];
echo $_SESSION['username'];
echo $_POST['username'];
}
header("location: createlogin.php"); //redirecting to first page
}
}
Something like this:
<input name="var" value="<?= isset($_SESSION['var']) ? $_SESSION['var'] : null ?>" />
Try the other way around. Linking the form-action to the current page, and if all fields are valid; redirect it to the next page (registration.php). This way you'd still have all the post-data, you can process everything that needs to be saved in the session- and you can redirect after all of the logic is done.
My two cent would be keep the same page to validate the content and for the form.
You can include other PHP files from a single page depending on if the form is valid.
This way, you keep the same $_POST between both pages and don't need to store the posted data in a session variable.
Otherwise, if you want to keep the same architecture, you need to use the $_SESSION variables instead of the $_POST ones in your input value, such as the answer by delboy.
Replace:
<?php if(isset($_POST['username'])) echo $_POST['username'];?>
With:
<?php if(isset($_SESSION['username'])) echo htmlspecialchars($_SESSION['username']); ?>
^ Note: htmlspecialchars is used to prevent a reflected XSS if the users enters " as username.
The problem is, your data posted to registration.php, so you can't get the posted value in your original file. You are trying to use $SESSION but that's not recommended, and not right. Your whole solution is wrong.
Forget about session and separated files, put everything to registration.php file together.
You can check if user posted or not with $_SERVER['REQUEST_METHOD'] variable.
if($_SERVER['REQUEST_METHOD'] == 'POST'){
print 'Something just posted';
}
PS: Don't forget secure the password before you store it! :)
Today I was trying to create a login page with PDO class in PHP. It worked fine but my problem is my input always have a "root" string value for my first input, and a password inside my second input. Why and how can I erase that? I tried to put a "placeholder" inside of each input, and it still doesn't replace it. Oh, by the way, my input has a "yellow" background...kinda weird. And when I erase it manually, they return white as usual..
This is my code :
<?php
include_once('user.php');
if (isset($_POST['submit'])) {
$name = $_POST['username'];
$pass = $_POST['password'];
$object = new User();
$object->Login($name,$pass);
}
?>
<html>
<head>
</head>
<body>
<form method="post" action="<?= $_SERVER['PHP_SELF']; ?>">
Username: <input type="text" name="username"><br><br>
Password: <input type="password" name="password"><br><br>
<input type="submit" name="submit" value="Log In">
</form>
</body>
</html>
Cache, or saved credentials problem.
Try:
<input type="text" name="username" value="" placeholder="" autocomplete="off">
I can set session variables and use them on another page. However when i try to use a simple contactform with a username and email address and try to store them into session variables, they don't show up on other pages. There must be something basic i'm missing.
Here's the form:
<?php
session_start();
$submit = $_POST["submit"];
if($submit){setSessionVars();}
function setSessionVars() {
$_SESSION['name'] = $_POST['name'];
$_SESSION['email'] = $_POST['email'];
header('Location: session.php');
}
?>
<html>
<body>
<form action="session.php" method"post">
<input name="name" type="text" value="Name" size="11" /><br />
<input name="email" type="text" value="Email" size="11" /><br /><br />
<input name="submit" type="submit" value="Submit" size="11" />
</form>
</body>
</html>
And this is session.php:
<?php
session_start();
echo $_SESSION['name'];
echo $_POST['name'];
?>
Also
header('Location: session.php');
is not working. Any ideas?
At a glance, I see one immediate problem that will keep the form from posting.
<form action="session.php" method"post">
You need an "=" sign between method and "post".
Changing that alone will give you the "t" in session.php.
You post the form to session.php:
<form action="session.php" method"post">
I'd change it to:
<form method="post">
That way, the page posts to itself. Then it can register the session variables and redirect the user to session.php.
Edit: also, you forgot the = sign in method"post".
In file1.php, I execute file2.php with
<form action="file2.php" method="POST">
Within file2, I want to access html elements from file1, but their document objects are different.
How can I access file1's elements from within file2?
well rather then using javascript you can access what you need via PHP's $_POST function so if you had the elements name and password you could access them with:
$_POST['name'];
$_POST['password'];
so something like this for file2.php:
<?php
if(array_key_exists('submit', $_POST))
{
$username = $_POST['name'];
$password = $_POST['password'];
echo("Hello $username, your password is $password");
}
?>
That goes on the assumption of file1.php looking like:
<form action="file2.php" method="post">
Username:
<input type="text" name="name" />
<br />
Password:
<input type="password" name="password" />
<br />
<input type="submit" name="submit" />
</form>
Why don't you try hiding in file2.php the elements that you still need?.
Just render them again with php in file2.php, and hide them via CSS, then they will be easily accesible through javascript.