Can't access session variables in php
up vote
0
down vote
favorite
I'm trying to use session variable $_SESSION['name']
on the second page i.e review.php
.
but it gives me nothing on the second page.How to use Session variable on the second page?
This is index.php
<?php
session_start();
$connect = mysqli_connect('localhost','root','root','review');
if(isset($_POST['submit']))
{
$id = $_POST['id'];
$_SESSION['name'] = $id;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>
login
</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
</head>
<body>
<form action="review.php">
<input type="text" name="id" placeholder="id">
<input type="password" name="password" placeholder="password">
<input type="submit" name="submit" class="btn btn-success">
</form>
</body>
</html>
here is review.php
<?php
session_start();
$connect = mysqli_connect('localhost','root','root','review');
if(isset($_SESSION['name']))
{
echo $_SESSION['name'];
}
else{
echo "nothing";
}
?>
php session
New contributor
add a comment |
up vote
0
down vote
favorite
I'm trying to use session variable $_SESSION['name']
on the second page i.e review.php
.
but it gives me nothing on the second page.How to use Session variable on the second page?
This is index.php
<?php
session_start();
$connect = mysqli_connect('localhost','root','root','review');
if(isset($_POST['submit']))
{
$id = $_POST['id'];
$_SESSION['name'] = $id;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>
login
</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
</head>
<body>
<form action="review.php">
<input type="text" name="id" placeholder="id">
<input type="password" name="password" placeholder="password">
<input type="submit" name="submit" class="btn btn-success">
</form>
</body>
</html>
here is review.php
<?php
session_start();
$connect = mysqli_connect('localhost','root','root','review');
if(isset($_SESSION['name']))
{
echo $_SESSION['name'];
}
else{
echo "nothing";
}
?>
php session
New contributor
it is like: localhost/php/review/…
– Khan
yesterday
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to use session variable $_SESSION['name']
on the second page i.e review.php
.
but it gives me nothing on the second page.How to use Session variable on the second page?
This is index.php
<?php
session_start();
$connect = mysqli_connect('localhost','root','root','review');
if(isset($_POST['submit']))
{
$id = $_POST['id'];
$_SESSION['name'] = $id;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>
login
</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
</head>
<body>
<form action="review.php">
<input type="text" name="id" placeholder="id">
<input type="password" name="password" placeholder="password">
<input type="submit" name="submit" class="btn btn-success">
</form>
</body>
</html>
here is review.php
<?php
session_start();
$connect = mysqli_connect('localhost','root','root','review');
if(isset($_SESSION['name']))
{
echo $_SESSION['name'];
}
else{
echo "nothing";
}
?>
php session
New contributor
I'm trying to use session variable $_SESSION['name']
on the second page i.e review.php
.
but it gives me nothing on the second page.How to use Session variable on the second page?
This is index.php
<?php
session_start();
$connect = mysqli_connect('localhost','root','root','review');
if(isset($_POST['submit']))
{
$id = $_POST['id'];
$_SESSION['name'] = $id;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>
login
</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
</head>
<body>
<form action="review.php">
<input type="text" name="id" placeholder="id">
<input type="password" name="password" placeholder="password">
<input type="submit" name="submit" class="btn btn-success">
</form>
</body>
</html>
here is review.php
<?php
session_start();
$connect = mysqli_connect('localhost','root','root','review');
if(isset($_SESSION['name']))
{
echo $_SESSION['name'];
}
else{
echo "nothing";
}
?>
php session
php session
New contributor
New contributor
New contributor
asked yesterday
Khan
155
155
New contributor
New contributor
it is like: localhost/php/review/…
– Khan
yesterday
add a comment |
it is like: localhost/php/review/…
– Khan
yesterday
it is like: localhost/php/review/…
– Khan
yesterday
it is like: localhost/php/review/…
– Khan
yesterday
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
You have 2 problem:
- You send submitted form to
review.php
, so you should read form data inreview.php
- You did not set form
method
. As defaultmethod
isGET
, but you read$_POST
variable. (What is the default form HTTP method?)
So you should change index.php
as follow:
<?php
session_start();
$connect = mysqli_connect('localhost','root','root','review');
if(isset($_POST['submit']))
{
$id = $_POST['id'];
$_SESSION['name'] = $id;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>
login
</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
</head>
<body>
<form method="post">
<input type="text" name="id" placeholder="id">
<input type="password" name="password" placeholder="password">
<input type="submit" name="submit" class="btn btn-success">
</form>
</body>
</html>
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You have 2 problem:
- You send submitted form to
review.php
, so you should read form data inreview.php
- You did not set form
method
. As defaultmethod
isGET
, but you read$_POST
variable. (What is the default form HTTP method?)
So you should change index.php
as follow:
<?php
session_start();
$connect = mysqli_connect('localhost','root','root','review');
if(isset($_POST['submit']))
{
$id = $_POST['id'];
$_SESSION['name'] = $id;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>
login
</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
</head>
<body>
<form method="post">
<input type="text" name="id" placeholder="id">
<input type="password" name="password" placeholder="password">
<input type="submit" name="submit" class="btn btn-success">
</form>
</body>
</html>
add a comment |
up vote
0
down vote
accepted
You have 2 problem:
- You send submitted form to
review.php
, so you should read form data inreview.php
- You did not set form
method
. As defaultmethod
isGET
, but you read$_POST
variable. (What is the default form HTTP method?)
So you should change index.php
as follow:
<?php
session_start();
$connect = mysqli_connect('localhost','root','root','review');
if(isset($_POST['submit']))
{
$id = $_POST['id'];
$_SESSION['name'] = $id;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>
login
</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
</head>
<body>
<form method="post">
<input type="text" name="id" placeholder="id">
<input type="password" name="password" placeholder="password">
<input type="submit" name="submit" class="btn btn-success">
</form>
</body>
</html>
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You have 2 problem:
- You send submitted form to
review.php
, so you should read form data inreview.php
- You did not set form
method
. As defaultmethod
isGET
, but you read$_POST
variable. (What is the default form HTTP method?)
So you should change index.php
as follow:
<?php
session_start();
$connect = mysqli_connect('localhost','root','root','review');
if(isset($_POST['submit']))
{
$id = $_POST['id'];
$_SESSION['name'] = $id;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>
login
</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
</head>
<body>
<form method="post">
<input type="text" name="id" placeholder="id">
<input type="password" name="password" placeholder="password">
<input type="submit" name="submit" class="btn btn-success">
</form>
</body>
</html>
You have 2 problem:
- You send submitted form to
review.php
, so you should read form data inreview.php
- You did not set form
method
. As defaultmethod
isGET
, but you read$_POST
variable. (What is the default form HTTP method?)
So you should change index.php
as follow:
<?php
session_start();
$connect = mysqli_connect('localhost','root','root','review');
if(isset($_POST['submit']))
{
$id = $_POST['id'];
$_SESSION['name'] = $id;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>
login
</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
</head>
<body>
<form method="post">
<input type="text" name="id" placeholder="id">
<input type="password" name="password" placeholder="password">
<input type="submit" name="submit" class="btn btn-success">
</form>
</body>
</html>
answered yesterday
Mohammad Ali Taqvazadeh
947
947
add a comment |
add a comment |
Khan is a new contributor. Be nice, and check out our Code of Conduct.
Khan is a new contributor. Be nice, and check out our Code of Conduct.
Khan is a new contributor. Be nice, and check out our Code of Conduct.
Khan is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238025%2fcant-access-session-variables-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
it is like: localhost/php/review/…
– Khan
yesterday