Jump to content

Recommended Posts

So yeah, I still cannot understand how exactly are sessions used. I tried it in the past, also just a few hours ago. To my luck, it all failed. Yes, I've been using tutorials, but still, I don't get it. :/ Anyways, I was wondering if anyone can help me out, or at least help me out make this work using a session:

 

Let's start off with the log in form:

.. Other html stuff .. (No php code above the form)

<form action="Logged.php" method="post">
<li>Name:<input type="text" name="User" /></li>
<li>Pass: <input type="password" name="Password" /></li>
<input type="submit" value="Login" />
<input type="reset" value="Clear" /></form>

.. Rest of html stuff.. (No php code below the form)

 

Ok, once you hit "Login", it takes you to the Logged.php page and it shows your statistics. Once you leave that page, you are signed out. I want to know how to stay connected ( logged ) on ALL pages I have.

Link to comment
https://forums.phpfreaks.com/topic/238614-help-understanding-php-sessions/
Share on other sites

SESSIONS is a superglobal.  Just like SERVER, COOKIE, POST and GET, it is saved for a period of time, whether it be in a URL or in a POST form submission... the superglobals are there.

 

The $_SESSION variable is a bit different though.  It's difference is that it retains everything in its memory until the user's browser is closed, but in order to access the information in it, you must call the PHP function: session_start

 

By calling this function you can:

- set values

- ... and retrieve values

 

The main difference in SESSIONS and COOKIES, is that:

- COOKIES are stored on the client's computer (the person with the browser)

- SESSIONS are stored on the webserver... AKA the website.

 

Once you have called session_start().. setting and getting SESSION variables are as easy as:

session_start();
$my_name = $_SESSION['fname'];
$_SESSION['fullname'] = $myname . " Smith";

Thanks for the info guys. Well, I'm just testing the functions, and I think I got something going.

 

<?php
session_start();

echo "Hi<br />";
if(!isset($_SESSION['Username']))
{
    echo "You are not logged in!";
    echo "<br />Please login below:";
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>» Test «</title>

</head>
<body>
<?php
if(!isset($_SESSION['Username']))
{
	echo "
	<form method='post' action='Logged.php'>
		<input type='text' value='' name='User' />
		<input type='password' name='Password' />
		<input type='submit' value='Login' />
		<input type='reset' value='Clear' />
	</form>";
}
else echo "Congratz! You're still logged in ";
?>

<?php

if(isset($_SESSION['Username']))
{
	echo "<br /><br />Log me out.... <a href='http://Loggedout.php'>CLICK ME </a>";
}
?>
</form>
</body>
</html>

That would be the main page.

 

Is that how its supposed to be? If so, let me know!!

 

It's currently working, but if there other methods in doing that, let me know!!

You've got it right,.. but my advice would be to set a variable in SESSION called is_logged or auth or something that let's you use a boolean value.

 

This way you can do this




session_start();



echo "Hi
";



if(isset($_SESSION['Username']) && $_SESSION['auth'])



{



    echo "You are not logged in!";



    echo "
Please login below:";
}
?>

But using that method, you should immediately have $_SESSION['auth'] set false by default until logged in... then set it true..

Doing this will allow you to know the last user logged in from the session... AND whether they are still logged in or not.  Because, it is possible for a person to login AND logout in one browser session.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.