Reputation: 5060
I have a php file (login.php) that processes a mysql connection,
then redirects to a sort of member's page after successful login.
That works flawlessly.
HOWEVER, when I attempt to include("login.php") and use, say the $username variable, it shows me the html of the redirect, or in this case the output of header("location:members.html");
This may not be a flaw, possibly a function of php, if so, should i maybe segregate the login.php file to two files, one that checks and one that redirects if successful?
thanks in advance
FILE INFO: login.html
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="login.php" method="post">
<table cellpadding=10>
<tr>
<td>Username:</td>
<td><input type="text" name="username"></input></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password"></input></td>
</tr>
<tr>
<td colspan="2"><input type="submit"></input></td>
</tr>
</table>
</form>
</body>
</html>
login.php
<?php
session_start();
$con = mysql_connect($host,$_POST['username'],$_POST['password']);
if(!$con)
{
die("Could Not Connect!" . "\n" . "Reason: " . mysql_error());
}
else
{
$_SESSION['username'] = $_POST['username'];
//header("Location:interact.html");
echo "<script>window.location = 'http://localhost/interact.html'</script>";
}
?>
interact.html
<html>
<head>
<title>Nexus | Envoy</title>
</head>
<body>
<p><?php echo "WELCOME ". $_SESSION['username']; ?></p>
</body>
</html>
Upvotes: 1
Views: 5616
Reputation: 1419
1.Slice the html and include header and footer file.
2.Change the content in accordance with the url requested e.g. keep one file say index.php and include header then its content and lastly footer. -say the request is index.php?content=register,then you will get the values of $_REQUEST['content'] and based on it you will include the content file ->like if $_REQUEST['content'] = 'register', include('register.php'). Register.php will contain the content only and no header and footer.
3.If $_REQUEST['content'] is empty , show home page.
Sample header.php:
<?php
session_start();
$con = mysql_connect($host,$_POST['username'],$_POST['password']);
if(!$con)
{
die("Could Not Connect!" . "\n" . "Reason: " . mysql_error());
}
if($_REQUEST['content'] == 'register') $title="Register";
if($_REQUEST['content'] == 'login') $title="Login";
..
?>
<html>
<head>
<title><?php echo $title; ?</title>
</head>
<body>
Sample footer.php
</body>
</html>
Sample index.php
<?php
require('header.php');
if(isset($_REQUEST['content']) && !empty($_REQUEST['content']))
{
if($_REQUEST['content'] == 'register') include('register.php');
if($_REQUEST['content'] == 'interact') include('interact.php');
if($_REQUEST['content'] == 'login') include('login.php');
..
}
else
{
include('home.php');
}
require('footer.php');
?>
Sample content file (login.php)
if(!empty($_POST))
{
$sql=mysql_query("select * from users where username = '".$_POST['username']."' and
password = '".$_POST['password']."' ");
if(mysql_num_rows > 0)
{
$_SESSION['username'] = $_POST['username'];
header("Location:interact.php");
}
else
{
echo "Invalid Username/password";
}
} ?>
<form action="login.php" method="post">
<table cellpadding=10>
<tr>
<td>Username:</td>
<td><input type="text" name="username"></input></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password"></input></td>
</tr>
<tr>
<td colspan="2"><input type="submit"></input></td>
</tr>
</table>
</form>
Upvotes: 0
Reputation: 1419
Firstly you should slice the html into header, content and footer. The content changes for each page and header and footer will remain same. Add session_start() and code to establish connection in the header file.
Just to give you a rough idea...
login.php
<?php
session_start();
$con = mysql_connect($host,$_POST['username'],$_POST['password']);
if(!$con)
{
die("Could Not Connect!" . "\n" . "Reason: " . mysql_error());
}
else
{
$sql=mysql_query("select * from users where username = '".$_POST['username']."' and
password = '".$_POST['password']."' ");
if(mysql_num_rows > 0)
{
$_SESSION['username'] = $_POST['username'];
header("Location:interact.php");
}
else
{
echo "Invalid Username/password";
}
}
?>
Login
Username: Password:
Interact.php
Nexus | Envoy ->Slice the html and include header and footer file. ->Change the content in accordance with the url requested e.g. keep one file say index.php and include header then its content and lastly footer. ->say the request is index.php?content=register,then you will get the values of $_REQUEST['content'] and based on it you will include the content file ->like if $_REQUEST['content'] = 'register', include('register.php'). Register.php will contain the content only and no header and footer. ->If $_REQUEST['content'] is empty , show home page. Sample header.php: Sample footer.php Sample index.phpUpvotes: 1