/    Sign up×
Community /Pin to ProfileBookmark

PHP Sessions; variables not being carried over

Hi,

I am trying to use PHP sessions, but I’m having a bit of a problem.

I created an [B]INDEX.php[/B] file, in which a new user can register for the first time.

In this file I have the following

(a) session_start()
(b) database connection
(c) definition for ALL variables
(d) an input form for those attributes required upon registration — login, password, email, firstname, surname

The registration goes smoothly. From there, the user is re-directed to the login page ([B]LOGIN.php[/B]), where he logs in for the first time.

This also works fine.

The problem is : the variables are not carried over (even though I specified [B]SESSION_START()[/B] at the top of the login.php file, and every other file which followed)

The user can log in, but the page does not show his data — such as “[B]Welcome, $firstname[/B]

I keep getting the error : [B]Variable “firstname” not defined[/B]

The same goes for each subsequent page………….from page to page, the variables are missing. They are not displayed. Only that annoying error message : “Variable not defined”.

I have tried to modify the “[B]$_SESSION[/B]” setting for each variable………..tried all possible ways and combinations, but nothing seems to work.

Here is my first file (the Index.php) file :

[code=php]<?php session_start();

$db_username = ‘xxxx’;
$db_password = ‘xxxxx’;
$db_name = ‘xxxxxx’;
$db_host = ‘localhost’;

$conn = new mysqli($db_host, $db_username, $db_password, $db_name);

$selected = mysqli_select_db($conn, “xxxxxxx”) or die(“Could not locate database getaqu : ” . mysqli_connect_error());

error_reporting(E_ALL & ~E_NOTICE);

if(isset($_POST[‘submit’])||isset($_POST[‘submit’])) {

$login = $_POST[‘login’];
$password = $_POST[‘password’];
$password2 = $_POST[‘password2’];
$firstname = $_POST[‘firstname’];
$surname = $_POST[‘surname’];
$email = $_POST[’email’];
$email2 = $_POST[’email2′];
$dateofbirth = $_POST[‘dateofbirth’];
$address = $_POST[‘address’];
$city = $_POST[‘city’];
$zipcode = $_POST[‘zipcode’];
$state = $_POST[‘state’];
$country = $_POST[‘country’];

$_SESSION[‘login’] = $_POST[‘login’];
$_SESSION[‘password’] = $_POST[‘password’];
$_SESSION[‘password2’] = $_POST[‘password2’];
$_SESSION[‘firstname’] = $_POST[‘firstname’];
$_SESSION[‘surname’] = $_POST[‘surname’];
$_SESSION[’email’] = $_POST[’email’];
$_SESSION[’email2′] = $_POST[’email2′];
$_SESSION[‘telephone’] = $_POST[‘telephone’];
$_SESSION[‘dateofbirth’] = $_POST[‘dateofbirth’];
$_SESSION[‘address’] = $_POST[‘address’];
$_SESSION[‘city’] = $_POST[‘city’];
$_SESSION[‘zipcode’] = $_POST[‘zipcode’];
$_SESSION[‘state’] = $_POST[‘state’];
$_SESSION[‘country’] = $_POST[‘country’];

$sql_insert = “INSERT INTO registration (login, password, email)
VALUES (‘$login’, ‘$password’, ‘$email’)”;

$result_insert = mysqli_query($conn,$sql_insert);

if ($result_insert) {

echo : ‘REGISTRATION COMPLETE ! Thank you’;

exit();
}

else {
echo “ERROR: Was not able to execute $sql. ” . mysqli_error($conn);
}
}
?>

[/code]

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@newuserphpauthorFeb 27.2015 — P. S. I am aware of the security issue (regarding [B]ESCAPING [/B]the attributes, and all that). That is not my concern at the moment. I'll sort that out later. I just need to get this SESSION thing fixed first. Thanks
Copy linkTweet thisAlerts:
@LandslydeFeb 27.2015 — You can't use $firstname on another page until you set it again.

You set it here on one page, the index.php page I guess:

[code=php]
$_SESSION['firstname'] = $_POST['firstname'];
[/code]


To use that on another page, you have to set $_SESSION['firstname'] to a variable:

[code=php]
$firstname = $_SESSION['firstname'];
[/code]


Then, on that page, you can use that session variable. OR you could just use:

[code=php]$_SESSION['firstname'] [/code]

and that will do it for you too. I hope I read your post right and this is what you're looking for.
Copy linkTweet thisAlerts:
@newuserphpauthorFeb 27.2015 — You can't use $firstname on another page until you set it again.

You set it here on one page, the index.php page I guess:

[code=php]
$_SESSION['firstname'] = $_POST['firstname'];
[/code]


To use that on another page, you have to set $_SESSION['firstname'] to a variable:

[code=php]
$firstname = $_SESSION['firstname'];
[/code]


Then, on that page, you can use that session variable. OR you could just use:

[code=php]$_SESSION['firstname'] [/code]

and that will do it for you too. I hope I read your post right and this is what you're looking for.[/QUOTE]



Thank you, Landslyde.

I tried it your way, but still not working.

Firstname is still not showing.

And, even when I tried to run an SQL query on the same file, like :

<php session_start();

// create database connection, etc, etc

$login = $_SESSION['login'];

$sql_test_query = "UPDATE registration SET email='xxxxxx' where login = $login

[/QUOTE]

This did not even work
Copy linkTweet thisAlerts:
@ginerjmFeb 27.2015 — Show us the Other script that doesn't work.
Copy linkTweet thisAlerts:
@newuserphpauthorFeb 27.2015 — This is my LOGIN.PHP

[code=php]<?php session_start();

function SignIn() { //starting the session for user profile page

$servername = "localhost";
$username = "xxxx";
$password = "xxxxxx";
$dbname = "xxxxxx";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("Unable to connect to MySQL. Connection failed: " . $conn->connect_error);
}


$selected = mysqli_select_db($conn, "xxxxxxxx") //select a database
or die("Could not locate database getaqu : " . mysqli_connect_error());


if(!empty($_POST['login']))

{

$query = mysqli_query($conn,("SELECT * FROM registration where
login = '$_POST[login]' AND password = '$_POST[password]'"))
or die(mysqli_error($conn));

$row = mysqli_fetch_array($query);

if(!empty($row['login']) AND !empty($row['password'])) {
$_SESSION['login'] = $row['password'];

echo "LOGIN SUCCESSFUL.";

exit();
}

else
{ echo "<p style='color:red; font=bold'><b>Sorry; the username and
password you entered are incorrect. Please try again...n<br /></p>"; }

}
}

if(isset($_POST['submit'])||isset($_POST['submit'])) {

SignIn(); }

?>

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Log in</title>

</head>

<body> <div id="Sign-In">
<fieldset style="width:30px"><legend><b>Please log in :</b></legend>
<form method="POST" action="login.php">
Username <br><input type="text" name="login" size="40" required><br>
Password <br><input type="password" name="password" size="40"
required>
<br>
<br>
<input id="button" type="submit" name="submit" value="Log In">
</form>
</fieldset>

</div>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@ginerjmFeb 27.2015 — I don't see ANY Session usage in this script at all. What is the problem here?
Copy linkTweet thisAlerts:
@rootMar 02.2015 — I don't see ANY Session usage in this script at all. What is the problem here?[/QUOTE]

People sometimes forget that things don't automatically happen!

As for the script, PHP sessions are best used for carrying a users ID and a screen / display name between pages and retrieve from a database the rest of the data.
Copy linkTweet thisAlerts:
@LandslydeMar 03.2015 — Although I'm new to PHP, I see a couple things that you might want to reconsider.

1) You start a session on the index.php page. You then redirect them, upon successful registration, to the login.php where you start yet another session. You should be getting a warning here, telling you your new call to start_session() is being ignored because you already have one going. Instead, on the login.php page, you should check to see if a session's going:

[code=php]
<?php
if(isset($_SESSION['id'])) {
exit();
} else {
session_start();
}
?>
[/code]


Pu that at the very top of your login.php page. That's #1.

2) I see nowhere in login.php a call to the $firstname variable. Since you set that on index.php, you only need to bring it back to life on this new page:

[code=php]
<?php
$firstname = $_SESSION['firstname'];
?>
[/code]


My knowlege is limited with PHP, but I'm fairly certain that will work for you.
Copy linkTweet thisAlerts:
@NogDogMar 03.2015 — One thing I see is the exit(); in your SignIn() function. At that point, PHP will quit processing and nothing beyond that point will get processed or displayed. Is that really what you want? (Maybe just [B][FONT=Courier New]return true;[/FONT][/B] instead?)
Copy linkTweet thisAlerts:
@LandslydeMar 03.2015 — One thing I see is the exit(); in your SignIn() function. At that point, PHP will quit processing and nothing beyond that point will get processed or displayed. Is that really what you want? (Maybe just [B][FONT=Courier New]return true;[/FONT][/B] instead?)[/QUOTE]

Yessssss ? Return true. Like I said, I'm a noob too. Just trying to help. I hope he got the general idea I was trying to convey. I think I'll leave the tough stuff to you guys that actually have a clue. Sorry for misleading you on that line of code, newuserphp. Wasn't intentional at all.
Copy linkTweet thisAlerts:
@newuserphpauthorMar 03.2015 — Thanks for all the helpful replies. ?
Copy linkTweet thisAlerts:
@rootMar 03.2015 — IF... after all that, it doesn't work, you must be expiring the cookies that the browser gets issued by the server to the browser, have you looked at excluding your domain from any blanket cookie ban?

If thats not the problem, then I would look in to your PHP install, is it set up properly? Have you looked in the php.ini file to ensure that libraries needed are either made availible or installed.
×

Success!

Help @newuserphp spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...