How to use the session in php for connecting a login-building-output page - php

I have 3 pages. After login, resume generating page is there in which we type in our details and they are stored in a mysql server. After submitting, i have used window.replace to go to the resume output page which have to retrieve the data from the database of the same resume that had been building just now. How to retrieve the data from the same row? How to do it efficiently using session? primary key of the login/register table is 'u_id', primary key of the resume table is 'res_id'.

To use sessions every page you use them in needs to have
session_start();
After writing this you can use $_SESSION. After login you can make it that the username gets saved in the session with eg.
$_SESSION['username'] = 'xxx';
$_SESSION['password'] = 'yyy';
$_SESSION['userId'] = 'zzz';

Related

PHP session is not corresponding to current value in database

I have user information that I stored in array session (only when user successfully login). When I update the user information using my web form, I realized that the user information that stored in session is not up to date with the database value.
Does that mean the session only memorizing those assigned data once till it meet with a function that replacing the data stored in session? So if I want to get the updated data from database, I need to keep re-assigned back the data session?
EDIT
I have this code in my Homepage_controller to fetch user information when they successfully login and store in session.
$_SESSION['user'] = $this->Homepage_model->fetchUser();
Then user will be redirected to profile page which is using different controller User_account_controller and from this controller I used to update user profile and will keep using the $_SESSION['user'] that been set from the Homepage_controller
As I can understand your post you are assigning session variables on login.
While you are logged in you change the data in database and want to update session variables?
If so you need to query database, retrieve new values and assign to session variables again.

pass a column value to be use with $_SESSION

I have a table with colname(INT) which i would like to pass its value to use with my $_SESSION variable
What i want to do:
I want all my protected pages not to viewed more than a certain time which would be determined by the value of a colname(INT) use with $_SESSION.
SOLUTION:
Just to determine/limit how long a user spends on all protected pages active or inactive.
You can try this link
How to set session timeout code in PHP
Also, I don't see a point why you are storing password in session (Register username and password as session variables). You can validate the login credentials from DB and only then store some data about the user in session.
OK as I understood your question you want to restrict a user to view a particular page only for few times.
You can do that by saving the views in database that is OK.
And you want to store the views in $_SESSION as the user login.
You can do that as follows:
if(login = success)
$_SESSION['viewCount'] = mysql_query("SELECT views FROM user WHERE userId=".$username.";");
I hope this will help

Browser-based RPG: Using sessions to store player data

I'm creating a browser Role Playing Game in JavaScript, HTML5 (canvas). I'd like to store player data in a MySQL database using sessions.
I've created a login system and it seems to be recognizing me when I log in.
session_start();
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
echo "Welcome back, " . $_SESSION['username'] . "!";
} else {
header("Location: php/Login.php");
}
Would I store data into a database using session variables as well? How do I associate the data with the username stored in the current session? Would I create session variables to store each item? That doesn't seem logical. When the user presses "save", it will call a MySQL function to load that data into a table.
When your login function (presumably) reads the username and login data to validate the user, it can also retrieve the userid or whatever you're using as the primary key for the user table (you are using a primary key, right?) Then you can store that key in the session itself for use in subsequent read and write operations.

How to use session variables to output logged in username and properties

I'm building an application and have just added a Login page which works well, communicating with a "username" and "password" field in the database. After logging in, it takes you a page users are authorized to see.
Is there a way I can display "Welcome, xxx" to the particular user logged in at the next page? And more importantly, output detailed information of the logged in user on that next page? More technically, to output properties of the username which are in different tables in the database.
Now I know I should begin with:
<?php
session_start();
$_SESSION['username'] = $user_name;
?>
But how do I specify assigning the value of "$user_name" to the value inputted in the username textbox.
Use this
$_SESSION['user_name'] = $_POST['username'];
I think using sessions is unnecessary for this task.
Simply search the database for the user's name once they log in and display this back to the page. Only store what's needed in sessions, databases are a more efficient storage medium.

PHP Login system like yahoo Messenger

I would create a login authentication using php, The requirement is : one username account can't login at the same time, if this occur the first login user should logout automatically. Just like yahoo messenger do. How is the concept actually ?, What the best trick to do this with PHP ?
Thank you.
You need to store the session ID of the last login in your database. When a user logs in the next time, you invalidate the old session and store the newly created session ID in the database. For example:
$old_sess_id = /* read saved session ID from database */;
session_id($old_sess_id);
session_start();
session_regenerate_id(true); // "true" deletes old session
$new_sess_id = session_id();
/* store new session ID in database */
casablanca is right...
additionally you dont need to save both old and new session. just have one session in your db.
when the user logs in update your session value. after code for check session. The previous login will be automatically invalidated.

Categories