PHP coding help!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

dhruvbhatia
Forum Newbie
Posts: 8
Joined: Mon Oct 31, 2011 3:39 am

PHP coding help!!

Post 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()));
}
?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP coding help!!

Post by social_experiment »

Code: Select all

include_once("main_login.php");
require("includes/main.php");
What do the pages do?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
dhruvbhatia
Forum Newbie
Posts: 8
Joined: Mon Oct 31, 2011 3:39 am

Re: PHP coding help!!

Post 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)
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP coding help!!

Post 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?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
dhruvbhatia
Forum Newbie
Posts: 8
Joined: Mon Oct 31, 2011 3:39 am

Re: PHP coding help!!

Post by dhruvbhatia »

yea it is used to login a user
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP coding help!!

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
dhruvbhatia
Forum Newbie
Posts: 8
Joined: Mon Oct 31, 2011 3:39 am

Re: PHP coding help!!

Post 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");

?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP coding help!!

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
dhruvbhatia
Forum Newbie
Posts: 8
Joined: Mon Oct 31, 2011 3:39 am

Re: PHP coding help!!

Post by dhruvbhatia »

So what should i do now?? what is the solution?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP coding help!!

Post by social_experiment »

What happens if you comment this line out : include_once("main_login.php");
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
dhruvbhatia
Forum Newbie
Posts: 8
Joined: Mon Oct 31, 2011 3:39 am

Re: PHP coding help!!

Post by dhruvbhatia »

Then the 2nd part works without the login part.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP coding help!!

Post by social_experiment »

Can the user login without it?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
dhruvbhatia
Forum Newbie
Posts: 8
Joined: Mon Oct 31, 2011 3:39 am

Re: PHP coding help!!

Post by dhruvbhatia »

NO...
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP coding help!!

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
dhruvbhatia
Forum Newbie
Posts: 8
Joined: Mon Oct 31, 2011 3:39 am

Re: PHP coding help!!

Post 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>
Post Reply