Jump to content

Sessions And Username


kelleydl

Recommended Posts

A user can log in and when they click submit takes them to a signin.php which just checks to make sure the user info is correct, and if so registers the username into a session. Then sends it back to the main page which checks to see if the user is registered in a session. I understand storing variable information, but how do I store the username which is registered into a variable? The w3schools tutorial shows how to increment page views into a variable but that doesn't help me understand how to retrieve the current username.

Link to comment
Share on other sites

Well, if the username is in a session then you can retrieve the username from said session variable.

Link to comment
Share on other sites

let me try to rephrase this.. when the signin.php checks if its correct and it is, i do this.. //username comes from $_POST[username]session_register(username);header("Location:http://www. ");then in my index.php i have...<?php session_start(); if(!session_is_registered(username)){ header("Location:http://www. "); } $_SESSION['user'] = username;?><html>i don't get how to use the username that was registered in the prior page to store in a variable on the session_start() for use in any of my pages.

Link to comment
Share on other sites

You shouldn't use session_register, that's the really old way to use the session. Just use the $_SESSION array instead, you write and read to the $_SESSION array like any other array. You can use isset to check if a variable has been set in the session.

Link to comment
Share on other sites

The way I do it is just assign the session variable like this://check $_POST["username"] is not empty, sanitize it, check it exists, perhaps update a table with when the user last signed in$_SESSION["username"] = $_POST["username"];session_write_close();then to checkif($_SESSION["username"]) {//whatever you want to do with it} else {//deal with it not being set}I've not had any problems with that whatsoever, and that's all there is to it.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...