 |
|
|
| View previous topic :: View next topic |
| Author |
Message |
curtranhome

Joined: 04 Jun 2008 Posts: 77
|
Posted: Mon Nov 23, 2009 8:46 pm Login and cookie script |
|
|
|
hey i'm trying to do a login script that checks if the form has been filled out, if it hasn't it displays the form then the form's action attribute sends it to the same page which then runs the same check form script but this time it validates it, when its done validating it directs it to a cookies page where it should make a cookie named 'username' with the name of the user set as the value of the cookie but it returns the username field as being empty, in turn, directs the user away from the page they were going to and the login page.
here is my 'login.php' code:
| Code: |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Login</title>
<link href="css/main.css" type="text/css" rel="stylesheet" />
</head>
<body>
<center>
<div id="maincontent">
<!-- ignore this - this is my navigation written in javascript -->
<script src="js/nav.js" type="text/javascript"></script>
<br />
<?php
$usernamepost = $_POST['name'];
$passworded = $_REQUEST['pass'];
$username = $_REQUEST['name'];
$samuser = "Sam";
$sampass = "bob";
$userpassmix = $username . $passworded;
if(isset($userpassmix)){
if($username==$samuser){
if($passworded==$sampass){
header("Location: http://www.hjwebs.com/freshfruit/cookies.php");
}
else{
echo("<span id='errortext'>Incorrect username or password</span><br />
<form name='login' action='login.php'>
Username: <input type='text' id='name' name='name' value='" . $username . "' /><br />
Password: <input type='password' id='pass' name='pass' /><br />
<input type='submit' value='Login' />
</form>
");
}
}
else{
echo("<span id='errortext'>Incorrect username or password</span><br />
<form name='login' action='login.php'>
Username: <input type='text' id='name' name='name' /><br />
Password: <input type='password' id='pass' name='pass' /><br />
<input type='submit' value='Login' />
</form>
");
}
}
else{
echo("
<form name='login' action='login.php' method='post'>
Username: <input type='text' id='name' name='name' /><br />
Password: <input type='password' id='pass' name='pass' /><br />
<input type='submit' value='Login' />
</form>");
}
?>
</div>
</center>
</body>
</html>
|
and here is my cookie code
| Code: |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
$name = $_REQUEST['name'];
if($name == "" || $name == " "){
header("Location: http://www.hjwebs.com/freshfruit/");
}
else{
setcookie("username", $name, time()+3600);
echo("Welcome " . $name . "!");
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
echo $_COOKIE['username'];
?>
Hello
</body>
</html>
|
I hope this is possible WITHOUT using database software
thanks ahead of time |
|
prophpscripts
Joined: 24 Nov 2009 Posts: 1
|
Posted: Tue Nov 24, 2009 9:46 am |
|
|
|
Hi curtranhome,
You need to "setcookie" and "header" before any html output is made because php will send it's headers when you do any output...and that will mean you cant change them. If you see what I mean?
If you are really struggling can I take this opportunity to recommend my login script? It can be used without a mySQL database...and you can effectivly accomplish what you are trying to do in a few lines of code.
http://www.prophpscripts.com/scripts/user-session-pro |
|
curtranhome

Joined: 04 Jun 2008 Posts: 77
|
Posted: Tue Nov 24, 2009 4:31 pm RE: |
|
|
|
Thanks for the links I'm gonna try it out - but for my own learn purposes, how can I make this script work? If it is too much trouble then please don't bother - I don't want anyone being too stressed over this
thanks,
curtranhome[/url] |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|
|
 |
|
|
|
|
|
|
|