Jump to content

help with creating a session


woodplease

Recommended Posts

hi.  as you will soon find out, i'm pretty useless at php. i need help creating a session to store a users name. i just need a simple text box where the user can enter his or her name and press an enter site button or something. i also want to be able to display the users name on each  page after that. i know how to open a session, its just making it so that the user enters their name i cant do. i have sorted out the logout page( a bit backwards i know). its just creating the initial session i cant seem to do.  Any help would be great.

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/183424-help-with-creating-a-session/
Share on other sites

Thanks for replying so quickly. This is what i've done now

 <form action="<?php
$_SESSION['USERNAME'] = $_POST['username'];
?>" method="post">
        <input type="text" name="name" id="username">
        <input type="submit" name="submit" value="Enter your name">
    </form>

 

If this is correct. how can i get it to display the username on other pages. i've tried this but it doesnt seem to work

 

<?php echo "User : ".$_SESSION['USERNAME']; ?>

 

Thanks

i've been told you can, if not how else could i do it?

 

try changing the name attribute first and then if that doesn't work then you can't use php code in there... if it does then you can. But the post has to read from the name attribute so thats one problem.

Take that php code out of the form is my advice. Make the form either submit to a new php file, like an execute php file where you take the post variable or use the isset function on that same file to check and see if the submit button is pressed with the following

 

isset($_POST['submit']);

use this code it has logout feature and everything :)

 

 

<?php
session_start()

if(isset($_POST['username'])) {

$_SESSION['username'] = S_POST['username'];
echo "You are now logged in";

}

if(isset($_GET['logout']) {

$logout = $_GET['logout'];

if($logout == "logout") {
$_SESSION = null;
unset($_SESSION['username'])

}
}
?>
<html>
<head>
<title> Login System </title>
</head>
<body>
<?php
if(isset($_SESSION['username'])) {
echo "Welcome ".$_SESSION['username']." to our site.";
}
?>
<br>
<center>
<h1>Website</h1>
</center>
<?php
if(!isset($_SESSION['username'])) {
?>
<form action="" method="post">
Username: <input type="text" name="username"><br>
<input type="submit" value="login">
</form>
<?php
}
?>
<br><br>
<?php
if(isset($_SESSION['username'])) {
?>
<form action="" method="get">
<input type="submit" name="logout" value="Logout">
</form>
<?php
}
?>
</body>
</html>

 

-John

Hey thanks for the code,  when i view the page with this code, i keep getting this error:

 

Parse error: syntax error, unexpected T_IF in /berw/ugrad1/base/a/agd8/public_html/gameshack/login.php on line 4

 

Any ideas?

 

Thanks

 

You forgot the ';'.

 

<?php
session_start();
?>

the code.....

 

<?php
session_start();

if(isset($_POST['username'])) {

$_SESSION['username'] = $_POST['username'];
echo "You are now logged in";

}

if(isset($_GET['logout']) {

$logout = $_GET['logout'];

if($logout == "logout") {
$_SESSION = null;
unset($_SESSION['username'])

}
}
?>
<html>
<head>
<title> Login System </title>
</head>
<body>
<?php
if(isset($_SESSION['username'])) {
echo "Welcome ".$_SESSION['username']." to our site.";
}
?>
<br>
<center>
<h1>Website</h1>
</center>
<?php
if(!isset($_SESSION['username'])) {
?>
<form action="" method="post">
Username: <input type="text" name="username"><br>
<input type="submit" value="login">
</form>
<?php
}
?>
<br><br>
<?php
if(isset($_SESSION['username'])) {
?>
<form action="" method="get">
<input type="submit" name="logout" value="Logout">
</form>
<?php
}
?>
</body>
</html>

here you go:

 

<?php
session_start();

if(isset($_POST['username'])) {

$_SESSION['username'] = $_POST['username'];
echo "You are now logged in";

}

if(isset($_GET['logout'])) {

$logout = $_GET['logout'];

if($logout == "logout") {

$_SESSION = null;
unset($_SESSION['username']);


}
}
?>
<html>
<head>
<title> Login System </title>
</head>
<body>
<?php
if(isset($_SESSION['username'])) {
echo "Welcome ".$_SESSION['username']." to our site.";
}
?>
<br>
<center>
<h1>Website</h1>
</center>
<?php
if(!isset($_SESSION['username'])) {
?>
<form action="" method="post">
Username: <input type="text" name="username"><br>
<input type="submit" value="login">
</form>
<?php
}
?>
<br><br>
<?php
if(isset($_SESSION['username'])) {
?>
<form action="" method="get">
<input type="submit" name="logout" value="logout">
</form>
<?php
}
?>
</body>
</html>

 

-John

Archived

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

×
×
  • 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.