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.
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
}
newbie here...
so yeah, i already tried searching all those page-related about my question, but im still stuck...
anyway, my problem is that i always keep getting back at my login page, which is my index.php
oh btw, im still using PHP version 4.4.8
here is my code for my problematic main page, main.php
<?php
session_start();
include '../config.php';
if(!isset($_SESSION['admin'])){
header("location:index.php");
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>KSP Setia Finance</title>
</head>
<body>
<h1>test page</h1>
</body>
</html>
and here is my login page code, which is index.php
<?php
session_start();
include '../config.php'; ?>
<!DOCTYPE html>
<html >
<head>
<title>Login Form</title>
</head>
<body>
<div class="login">
<h1>Login</h1>
<form action="login_act.php" method="post">
<input type="text" name="username" placeholder="Username" required="required" />
<input type="password" name="password" placeholder="Password" required="required" />
<button type="submit" name="login" value="Login" class="btn btn-primary btn-block btn-large">Log In</button>
</form>
</div>
<script src="js/index.js"></script>
</body>
</html>
since everyone asking, here my login_act.php, already inserted with session_start
<?php
session_start();
include('../config.php');
if(isset($_POST['login'])){
$user = mysql_real_escape_string(htmlentities($_POST['username']));
$pass = mysql_real_escape_string(htmlentities(md5($_POST['password'])));
$sql = mysql_query("SELECT * FROM user WHERE username='$user' AND password='$pass'") or die(mysql_error());
if(mysql_num_rows($sql) == 0){
echo 'User not found';
}else{
$row = mysql_fetch_assoc($sql);
if($row['level'] == 1){
$_SESSION['admin']=$user;
echo '<script language="javascript">alert("u are Login as Admin!"); document.location="index.php";</script>';
}else
echo 'sorry, u cant access this one';
}
}
?>
print value of $_SESSION on main.php and check if there is any key as 'username' and check login.php, what values are you storing in $_SESSION array
so i recently asking my friends, and here is the results:
all i need is just put those $SESSION_START above all, or make another php and link them all. so here my latest result that worked :
main.php
<?php
include 'access.php';
?>
<!DOCTYPE HTML>
<html>
<head>
<title>KSP Setia Finance</title>
</head>
<body>
<h1>test page</h1>
</body>
</html>
access.php
<?php
session_start();
if(!isset($_SESSION['admin'])){
echo '<script language="javascript">alert("you must Login!"); document.location="../index.php";</script>';
}
?>
and last, config.php
<?php
session_start();
mysql_connect("localhost","root","");
mysql_select_db("koperasi");
?>
i deleted that broken login_act.php, and making all the page i had to be linked directly with the access.php, which make it easier to manage the session. thank you to all that bear with my php problem and stupidity. hope this all gonna help those who still wandering and asking the same question.
this is the code for my html page in the site:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>index</title>
</head>
<body>
<html>
<body>
<form action="Untitled-4.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
</body>
</html>
and this is my code for the "Untitled-4.php" file:
<html>
<body>
<?php
if ($_GET["name"] == "tom"){
echo "hello you are special";
}
?>
</body>
</html>
this is the error i am getting:
"undefined index on line 7 of the php file"
I am trying to use the GET method to take whatever the user types in the "name" box to be used in the if statement that echoes "you are special" if the user types in "tom". can anyone tell me what the problem here is.
(the question stack overflow is saying is a possible duplicate is a completely different question)
Check if the variable is set first:
<html>
<body>
<?php
if (isset($_GET["name"])) {
if ($_GET["name"] == "tom"){
echo "hello you are special";}
} else {
echo "you are not special";
}
?>
</body>
</html>
for some reason i just tried it again with no changes to my code and it appears to be working i did not actually have to do anything.
i'm a little lost working without cookies.
I want to create a session passing the SID through the url, but i don't know how to pass and get data from another page.
I've googled a lot but 90% of the examples are with cookies.
Here is what i have.
index.php
<?php
ini_set("session.use_cookies",0);
ini_set("session.use_only_cookies",0);
ini_set("session.use_trans_sid",1);
session_start();
?>
<html>
<head>
<title>Index</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<link rel="STYLESHEET" type="text/css" href="style.css">
</head>
<body>
Index
Second
<form action="access.php" method="POST">
User: <input type="text" name="user" size="15"/><br>
Pass: <input type="password" name="pass" size="15"/><br>
<button type="submit"/>Ok</button>
<button type="reset"/>Reset</button>
</form>
Logged as: <?php print $_SESSION["name"]; ?>
</body>
</html>
access.php last part
........
.......
....
if($count==1){
// Register $myusername and redirect to file "second.php"
ini_set("session.use_cookies",0);
ini_set("session.use_only_cookies",0);
ini_set("session.use_trans_sid",1);
session_name('test');
session_start();
$_SESSION['name'] = $myusername;
header("location:second.php?".SID);
exit;
}
else {
echo "Wrong Username";
}
ob_end_flush();
?>
second.php
<?php
ini_set("session.use_cookies",0);
ini_set("session.use_only_cookies",0);
ini_set("session.use_trans_sid",1);
session_start();
?>
<html>
<head>
<title>Second</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<link rel="STYLESHEET" type="text/css" href="style.css">
</head>
<body>
Index
Second<br>
Logout<br>
Logged as: <?php print $_SESSION["name"]; ?>
</body>
</html>
logout.php
<?php
ini_set("session.use_cookies",0);
ini_set("session.use_only_cookies",0);
ini_set("session.use_trans_sid",1);
session_start();
session_unset();
session_destroy();
header('Location: index.php');
exit;
?>
-What do i have to put in "Logged as:" ?. "print $_SESSION["name"];" shows nothing.
-When i log in, i'm redirected to second.php, then i click on any link and the actual logged session dies and SID changes.
thx!
I just copied your code and tested it out myself. Everything is good but do not use, session_name('test') in your access.php file. Not really sure what that does but it breaks when I have it included. Instead, I used the $_SESSION['name'] without calling the session_name() function and all is working.
In order to pass anything through a URL you must use the proper syntax:
your.url.com/?key=value&key2=value2... and so on
Then to retrieve this data:
echo $_GET['key'] and echo $_GET['key2']
On one PHP server I have two files. One file (the name is "first.php") contains this code:
<html>
<head>
<title>First Page</title>
</head>
<body>
Please enter your password and age:
<form action="pass.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
The other file ("pass.php") contains this code:
<html>
<head>
<title>Secon Page</title>
</head>
<body>
<?php
if ($fname=="Jack")
echo "You are Jack!";
else
echo "You are not Jack!";
?>
</body>
</html>
As far as I understand, if a user enters "Jack" in the first page, than the second page should be displayed with "You are Jack!" line, but it doesn't happen. Why is it so?
On your second page, instead of checking for $fname, check for $_POST['fname'] instead. I think that is all you are missing.
You probably don't have register_globals set. This is depreciated and will be removed in 6.x. So for good programming you should instead of $fname try $_POST['fname'] on your second page.
pass.php needs to look like this
<html>
<head>
<title>Secon Page</title>
</head>
<body>
<?php
if ($_POST['fname'] =="Jack")
echo "You are Jack!";
else
echo "You are not Jack!";
?>
</body>
</html>
It might help to set the post values as variables and work with that. Something like this:
foreach($_POST as $key => $value)
{
$$key = $value;
}
Then whatever is posted will be available rather than using $_POST['xxxxx'] in your logic.