Page 1 of 2

PHP coding help!!

Posted: Mon Oct 31, 2011 3:47 am
by dhruvbhatia
Below is the code of index.php file. My motive is to first login by entering username and password. If the login details are correct then this includes/main.php part should run. If i include one at a time between main_login.php and main.php in index.php, then this thing works fine. But when I am including these both together, the login page is displayed for a second and automatically the main.php page is displayed. How can i rectify this thing? What should i do?
<?php
include_once("main_login.php");
require("includes/main.php");
try {

if($_GET['category']){
$c = new CategoryController();
}
else if(empty($_GET)){
$c = new HomeController();
}
else throw new Exception('Wrong page!');

$c->handleRequest();
}
catch(Exception $e) {
// Display the error page using the "render()" helper function:
render('error',array('message'=>$e->getMessage()));
}
?>

Re: PHP coding help!!

Posted: Mon Oct 31, 2011 3:52 am
by social_experiment

Code: Select all

include_once("main_login.php");
require("includes/main.php");
What do the pages do?

Re: PHP coding help!!

Posted: Mon Oct 31, 2011 4:18 am
by dhruvbhatia
main_login.php is a simple login page which takes the username and password. main.php contains statements like require_once(names of controllers, models, views)

Re: PHP coding help!!

Posted: Mon Oct 31, 2011 4:26 am
by social_experiment
dhruvbhatia wrote:main_login.php is a simple login page which takes the username and password.
This explains why it is only displayed briefly, i assume you use this to login the user?

Re: PHP coding help!!

Posted: Mon Oct 31, 2011 4:29 am
by dhruvbhatia
yea it is used to login a user

Re: PHP coding help!!

Posted: Mon Oct 31, 2011 4:35 am
by social_experiment
That page is usually called by the submit form, why do you include it again? After successfull login the user is directed to an authenticated-users-only area and those pages include other relevant pages

Re: PHP coding help!!

Posted: Mon Oct 31, 2011 4:46 am
by dhruvbhatia
This is the main.php file. Now can u tell me in detail as what should I do by making the changes wherever required. This 2nd part is done using MVC pattern whose files are included in main.php.
<?php
require_once "includes/config.php";
require_once "includes/connect.php";
require_once "includes/helpers.php";
require_once "includes/models/product.model.php";
require_once "includes/models/category.model.php";
require_once "includes/controllers/home.controller.php";
require_once "includes/controllers/category.controller.php";


// This will allow the browser to cache the pages of the store.

header('Cache-Control: max-age=3600, public');
header('Pragma: cache');
header("Last-Modified: ".gmdate("D, d M Y H:i:s",time())." GMT");
header("Expires: ".gmdate("D, d M Y H:i:s",time()+3600)." GMT");

?>

Re: PHP coding help!!

Posted: Mon Oct 31, 2011 5:08 am
by social_experiment
The problem imo is not with the file or it's contents but with the fact that you are calling the login process page before you call the other page

Re: PHP coding help!!

Posted: Mon Oct 31, 2011 5:10 am
by dhruvbhatia
So what should i do now?? what is the solution?

Re: PHP coding help!!

Posted: Mon Oct 31, 2011 5:31 am
by social_experiment
What happens if you comment this line out : include_once("main_login.php");

Re: PHP coding help!!

Posted: Mon Oct 31, 2011 5:33 am
by dhruvbhatia
Then the 2nd part works without the login part.

Re: PHP coding help!!

Posted: Mon Oct 31, 2011 5:35 am
by social_experiment
Can the user login without it?

Re: PHP coding help!!

Posted: Mon Oct 31, 2011 5:43 am
by dhruvbhatia
NO...

Re: PHP coding help!!

Posted: Mon Oct 31, 2011 5:48 am
by social_experiment
Ok. I don't understand the logic of the script because main_login should only be called once, from the form that requests user input; after that you have no need for it. Can you paste the code inside main_login.php

Re: PHP coding help!!

Posted: Mon Oct 31, 2011 5:50 am
by dhruvbhatia
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>