View Full Version : php sessions
burningr
06-09-03, 07:10 PM
can anybody help me with this problem?
I have this script, it should activate a session if i'm typing in "brukernavn" & "passord" correctly
<?php
if($_POST["b"] == "Haavard" && $_POST["p"] == "bo") {
session_start();
session_register('innlogget');
?>
<html>
<head>
<title>Du er logget inn</title>
<link rel="stylesheet" href="../css/basic.css">
</head>
<body>
<hr>
<h1>Du er nå logget inn, <?php echo $_POST["b"]; ?></h1>
<hr>
<h2><a href="medlemsider.php" title="Medlemsider">Trykk Her</a></h2>
</body>
</html>
<?php
}
elseif($_POST["b"] == "Mathias" && $_POST["p"] == "bo") {
session_start();
session_register('innlogget');
?>
<html>
<head>
<title>Du er logget inn</title>
<link rel="stylesheet" href="../css/basic.css">
</head>
<body>
<hr>
<h1>Du er nå logget inn, <?php echo $_POST["b"]; ?></h1>
<hr>
<h2><a href="medlemsider.php" title="Medlemsider">Trykk Her</a></h2>
</body>
</html>
<?php
}
else {
echo "du er ikke logget inn";
}
?>
but i'm not shure it is because when i take it to this script :<?PHP
session_start();
if (!isset($_SESSION['innlogget']) || $_SESSION['innlogget']!= true)
{
header('Location: index.php');
}
?>
it sends me right back to index.php when it's supose to let me trough if i have the right session, can anyone help me pls?
amailer
06-09-03, 07:27 PM
Ok, so this script above, the first one does it have to let both those names:
Mathias and Haavard to login? but if the password is wrong [ i think this is a login script.. ] they get redirected to index.php? or do they get the message: du er ikke logget inn
burningr
06-09-03, 07:41 PM
they get the message : du er ikke logget inn if they type the wrong password, but then i include script nr.2 in the pages i want to passwd protect end even if i have typed in right username & passwd & the session should have been registerd it sends me right back to index.php
amailer
06-09-03, 07:44 PM
Ok... i don't know lol but see i made it work, i think.
oh 1 thing, not think lol, the sessions are no use really here but if you want me to make the use i could lol
<?
if(!isset($b)) {
form();
} else {
if(($_POST["b"] == "Haavard" && $_POST["p"] == "bo")
OR ($_POST["b"] == "Mathias" && $_POST["p"] == "bo")) {
session_register("b", "p");
?>
<html>
<head>
<title>Du er logget inn</title>
<link rel="stylesheet" href="../css/basic.css">
</head>
<body>
<hr>
<h1>Du er nå logget inn, <?php echo $_POST["b"]; ?></h1>
<hr>
<h2><a href="medlemsider.php" title="Medlemsider">Trykk Her</a></h2>
<br>
<a href="<? echo "$PHP_SELF?login=logout&check=yes"; ?>" title="Logout?"> Log out </a>
</body>
</html>
<?php
} else {
//echo "du er ikke logget inn";
echo "Sorry, your username or password was incorrect, please try again.";
}
}
function form() {
global $PHP_SELF;
?>
<p> </p>
<p> </p>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%">
<tr>
<td width="100%">
<p align="center"><font face="Courier"><b>Admin Login:</b></font></td>
</tr>
</table>
<form method="POST" action="<?=$PHP_SELF?>">
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%">
<tr>
<td width="32%"> </td>
<td width="11%"><font size="2" face="Courier New">Username:</font></td>
<td width="60%"><font size="2">
<input type="text" name="b" size="20" style="font-family: Courier New; font-size: 8pt; border: 1px solid #FFFFFF; background-color: #C0C0C0"> </font></td>
</tr>
<tr>
<td width="32%"> </td>
<td width="11%"><font size="2" face="Courier New">Password:</font></td>
<td width="60%"><font size="2">
<input type="password" name="p" size="20" style="font-family: Courier New; font-size: 8pt; border: 1px solid #FFFFFF; background-color: #C0C0C0"></font></td>
</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%">
<tr>
<td width="43%"> </td>
<td width="11%">
<input type="submit" value="Submit" name="B3" style="font-family: Courier New; font-size: 8pt; border: 1px solid #FFFFFF; background-color: #C0C0C0"></td>
<td width="46%">
<input type="reset" value="Reset" name="B4" style="font-family: Courier New; font-size: 8pt; border: 1px solid #FFFFFF; background-color: #C0C0C0"></td>
</tr>
</table>
</form>
<?php
}
if(isset($login)) {
function logout() {
global $ub, $p, $PHP_SELF, $check;
if($check == "yes") {
session_unregister("p");
session_unregister("b");
echo "<p align=\"center\"><b>You have successfully logged out</b></p>";
echo "<p align=\"center\"><a href=\"index.php\" style=\"text-decoration: none\">";
echo "<font color=\"#071378\">Please click here to return to index.php.</font></a></p>";
} else {
echo "<h2>UNKNOWN URL</h2>";
}
}
switch ($login) {
case "logout":
logout();
break;
}
}
?>
amailer
06-09-03, 07:51 PM
Ok, i must tell you im a noob @ php, but i got another login [ i made its very cheap!, its used for amailer lol, Btw amailer is my script ] it uses MYSQL to store the DB, only for 1 user, the admin, im working on makign it for multiple users, but later, i gatta finish my site, if you want to see it, i would be glad to show you :)
burningr
06-09-03, 08:55 PM
im not shure i understan what you mean, but thats alright
I guess I'll figure it out somehow
Thanks
amailer
06-09-03, 08:58 PM
hehe i was not sure what the script had to do, so i just did what i thought it needed to do lol, but anyway
burningr
06-09-03, 09:00 PM
i need a script that cheks if the session['innlogget'] exists and if it exists, do nothing but if it don't exists send it back to index.php
amailer
06-09-03, 09:02 PM
ohhh! omg LOL! well i jsut thoughyou needed a login!
so, ill just use the login to do what you want it to do, check if the nnlogget exists, and etc etc, hehe sorry i just did not get it at first
burningr
06-09-03, 09:04 PM
I'll just be happy if someone can help me
Try setting values to the session vars:
<?php
if($_POST["b"] == "Haavard" && $_POST["p"] == "bo") {
session_start();
session_register('innlogget');
$_SESSION['innlogget'] = true;
?>
<html>
<head>
<title>Du er logget inn</title>
<link rel="stylesheet" href="../css/basic.css">
</head>
<body>
<hr>
<h1>Du er nå logget inn, <?php echo $_POST["b"]; ?></h1>
<hr>
<h2><a href="medlemsider.php" title="Medlemsider">Trykk Her</a></h2>
</body>
</html>
<?php
}
elseif($_POST["b"] == "Mathias" && $_POST["p"] == "bo") {
session_start();
session_register('innlogget');
$_SESSION['innlogget'] = true;
?>
<html>
<head>
<title>Du er logget inn</title>
<link rel="stylesheet" href="../css/basic.css">
</head>
<body>
<hr>
<h1>Du er nå logget inn, <?php echo $_POST["b"]; ?></h1>
<hr>
<h2><a href="medlemsider.php" title="Medlemsider">Trykk Her</a></h2>
</body>
</html>
<?php
}
else {
echo "du er ikke logget inn";
}
?>
amailer
06-09-03, 11:14 PM
From where did you get the variable B and P?
Originally posted by amailer
From where did you get the variable B and P?
Well, its form the POST array, so that means he got it from a form.
amailer
06-09-03, 11:23 PM
Really i must get used to PHP, i gatta get good at it!:
Goes in any file, try test2.php the secured file.. i think
<?
session_start();
if(isset($_SESSION["innlogget"])) {
echo "Doing nothing here...";
} else {
header("Location: index.php");
}
?>
HEHE WEL...
And this in the login file, JUST saying if this is a login script..
Try file name as: Test.php
<?
if(!isset($b)) {
form();
} else {
if(($_POST["b"] == "Haavard" && $_POST["p"] == "bo")
OR ($_POST["b"] == "Mathias" && $_POST["p"] == "bo")) {
session_register("b", "p");
session_register("innlogget");
$_SESSION['innlogget'] = true;
?>
<html>
<head>
<title>Du er logget inn</title>
<link rel="stylesheet" href="../css/basic.css">
</head>
<body>
<hr>
<h1>Du er nå logget inn, <?php echo $_POST["b"]; ?></h1>
<hr>
<h2><a href="medlemsider.php" title="Medlemsider">Trykk Her</a></h2>
<br>
<a href="<? echo "$PHP_SELF?login=logout&check=yes"; ?>" title="Logout?"> Log out </a>
</body>
</html>
<?php
} else {
//echo "du er ikke logget inn";
echo "Sorry, your username or password was incorrect, please try again.";
}
}
function form() {
global $PHP_SELF;
?>
<p> </p>
<p> </p>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%">
<tr>
<td width="100%">
<p align="center"><font face="Courier"><b>Admin Login:</b></font></td>
</tr>
</table>
<form method="POST" action="<?=$PHP_SELF?>">
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%">
<tr>
<td width="32%"> </td>
<td width="11%"><font size="2" face="Courier New">Username:</font></td>
<td width="60%"><font size="2">
<input type="text" name="b" size="20" style="font-family: Courier New; font-size: 8pt; border: 1px solid #FFFFFF; background-color: #C0C0C0"> </font></td>
</tr>
<tr>
<td width="32%"> </td>
<td width="11%"><font size="2" face="Courier New">Password:</font></td>
<td width="60%"><font size="2">
<input type="password" name="p" size="20" style="font-family: Courier New; font-size: 8pt; border: 1px solid #FFFFFF; background-color: #C0C0C0"></font></td>
</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%">
<tr>
<td width="43%"> </td>
<td width="11%">
<input type="submit" value="Submit" name="B3" style="font-family: Courier New; font-size: 8pt; border: 1px solid #FFFFFF; background-color: #C0C0C0"></td>
<td width="46%">
<input type="reset" value="Reset" name="B4" style="font-family: Courier New; font-size: 8pt; border: 1px solid #FFFFFF; background-color: #C0C0C0"></td>
</tr>
</table>
</form>
<?php
}
if(isset($login)) {
function logout() {
global $ub, $p, $PHP_SELF, $check;
if($check == "yes") {
session_unregister("p");
session_unregister("b");
echo "<p align=\"center\"><b>You have successfully logged out</b></p>";
echo "<p align=\"center\"><a href=\"index.php\" style=\"text-decoration: none\">";
echo "<font color=\"#071378\">Please click here to return to index.php.</font></a></p>";
} else {
echo "<h2>UNKNOWN URL</h2>";
}
}
switch ($login) {
case "logout":
logout();
break;
}
}
?>
Maby not what you wanted, BUT HEY! hehe i jsut ... ya LOL!
amailer
06-09-03, 11:26 PM
oh well :()
http://termix-zero.ath.cx/scripts/amailers/tsearch/test.php
might not be what you want BUT OH WELL IM LEARNING!
Looks like he's alread got the form all figured out and stuff. Personally, I'd write my script like this.
<?php
// Script1
$HTML = <<<HTML
<html>
<head>
<title>Du er logget inn</title>
<link rel="stylesheet" href="../css/basic.css">
</head>
<body>
<hr>
<h1>Du er nå logget inn, {$_POST["b"]} </h1>
<hr>
<h2><a href="medlemsider.php" title="Medlemsider">Trykk Her</a></h2>
</body>
</html>
HTML;
if(($_POST["b"] == "Haavard" && $_POST["p"] == "bo") || ($_POST["b"] == "Mathias" && $_POST["p"] == "bo"))
{
session_start();
session_register('innlogget');
$_SESSION['innlogget'];
echo $HTML;
}
else
{
echo "du er ikke logget inn";
}
// Script2
session_start();
if (!isset($_SESSION['innlogget']) || $_SESSION['innlogget']!= true)
{
// Just to make sure =D
// Might cause a problem with sending the location header though, if it does, you can just delete this line =)
@session_unregister('innlogget');
header('Location: index.php');
}
?>
(PHP code looks ugly on these forums, its all spaced out.. ?)
amailer
06-09-03, 11:42 PM
nice, is that what it needs to do...oh man lol
burningr
06-10-03, 03:59 PM
amailer, your script i think is wxactly what i need, if i need anymore help i'll post it here
amailer
06-11-03, 09:15 PM
np
ridwank
06-12-03, 12:38 AM
I think AMAILER have made a good script for you, why don't you use his script, rather yours (which doesn't worked)
It seem like you both misunderstanding each other.
and you (burningr) does not tell what is the problem exactly.
But sorry I can not help too...
amailer
06-12-03, 01:01 AM
i think he wanted a login, so that is a VERY not secure login, but it works still :)
The only problem with your script burningr was you weren't setting the session variables to anything, so when you were testing them to be 'isset' they returned false because they were unset. Simply giving a value to them would have solved your problem.
digitalsea
06-12-03, 08:07 AM
Have you tried looking under PHP > Scripts and Programs > User Authentication.
Those scripts do the same thing - block out content unless a session is opened or they see a cookie.
I know of a script in Perl that also does the same thing you want to do....
vBulletin® v3.6.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.