Skip to content

Instantly share code, notes, and snippets.

@brijeshb42
Created April 19, 2014 19:40
Show Gist options
  • Select an option

  • Save brijeshb42/11095165 to your computer and use it in GitHub Desktop.

Select an option

Save brijeshb42/11095165 to your computer and use it in GitHub Desktop.
session
<?php
$e = "";
if(isset($_POST) && isset($_POST['username']) && !empty($_POST['username']) && isset($_POST['password']) && !empty($_POST['password']) && $_POST['password']=='secret'){
session_start();
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
if(isset($_POST['football']) && !empty($_POST['football'])){
$_SESSION['football'] = $_POST['football'];
}
header('Location: welcome.php');
}else{
$e .= "Please enter a username and the correct password.";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<?php
if($e!=""){
?>
<p><?php echo $e;?></p>
<?php
}
?>
<p></p>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<label for="username">Username</label>
<input type="text" id="username" name="username" />
<br />
<label for="password">Password</label>
<input type="password" id="password" name="password" />
<br />
<label for="football">Football team</label>
<input type="text" id="football" name="football" />
<br />
<input type="submit" value="Submit">
</form>
</body>
</html>
<?php
$u = "";
$f = "";
session_start();
if(isset($_SESSION['username']) && !empty($_SESSION['username'])){
$u = $_SESSION['username'];
if(isset($_SESSION['football']) && !empty($_SESSION['football'])){
$f = $_SESSION['football'];
}
}else{
header('Location: login.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Welcome <?php echo $u; ?></title>
</head>
<body>
<h1>Welcome <?php echo $u; ?></h1>
<?php
if($f!=""){
?>
<h2>Your football team is: <?php echo $f;?></h2>
<?php
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment