PHP login 2 scripts

I have one php script that is in a folder protected by another php script.  Both have their own registration page and login page.  In my scenario, if users can login to the first, I don't really need them to login to the second.  
The username is the only common credential between the two user tables so is there a way to
"tell" the second script in protected folder that if user is logged into the first, then open that users account here?  

Thanks.

Large_FarvaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TiberiuGalCommented:
hi,
I guess you are already using session for the logged in user in both pages, so you should have a comon session variable.

you can use something like the code below in both files, or you could add it to a separate file which you will require in both files;
<?php 
session_start();
if( !empty($_POST['username']) && !empty($_POST['password']) ) 
   login();

if( empty($_SESSION['credentials']) ) 
    showLoginForm();


function login() {
  // if login ok set $_SESSION['credentials']
  // else unset($_SESSION['credentials']);
}

Open in new window

0
Mohamed AbowardaSoftware EngineerCommented:
You need to use SESSION variables to perform that.

When the user login on script 1, you can set SESSION variable that indicate that the user logged in using script 1.

For instance (add this code after successful login in script 1):
session_start();
$_SESSION['loginScript'] = 1;

Open in new window


On the other script, you can check if the user is logged in using the script 1 or not:
session_start();
if ($_SESSION['loginScript'] == 1)
{
    // User logged in from script 1
}

Open in new window

0
Large_FarvaAuthor Commented:
Thanks for the help!

Ok...I am extremely new to sessions so please forgive my ignorance here.  

To clarify, the login form of the first script redirects to the second script on successful login already but I am met with another login form once there.    

@TiberiuGal: which files would I add the code to?  index.php in second script only or do I need it somewhere in the first scripts login/processing page as well?

I did find that once you login to the first script, it sets a cookie "site_login" with contents of "yes".
Not sure if that helps.  

Thanks!

0
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Mohamed AbowardaSoftware EngineerCommented:
On script 1, when the user login successfully add the following code:
session_start();
$_SESSION['loginScript'] = 1;
header("location: /script2.php");

Open in new window


Change "/script2.php" with your second script full directory to redirect the user.

On script 2, add the following code to check if the user logged in using script 1:
session_start();
if ($_SESSION['loginScript'] == 1)
{
    // User logged in from script 1
}

Open in new window


If you still don't know how to do that, you can post the code here and I will modify it for you.
0
Large_FarvaAuthor Commented:
Medo3337, the login form already redirects to the right place so I don't think I need that part, sorry if my last post was misleading.

I don't understand how the second script will know who logged in so I can load their account data.  The first login as it is now already gives them access to the locked folder but I need it to also log them in on the second script as the same username as the first.  

I also don't know which files would need to be edited, so it is difficult for me to post the code.  Do you need the login page of first script and landing page of second?

Thanks.  


0
Mohamed AbowardaSoftware EngineerCommented:
If you could post the two scripts, I will modify them to work as you wish.

Also, I think you need to understand what's SESSION and why it's used.

Session is a way to store data in the SERVER memory, the server delete it and it become useless after specific amount of client inactivity time according to server configuration.

When you use session, you will be able to store a variable and read it from ANY PHP script on the website.

If you stored the SESSION variable using login.php, you can read it from any other script such as (index.php, member.php, signup.php, etc...), and that's why you use session variables instead of normal variables.

You must add the code "session_start();" before you use session variables on any PHP script.
0
Large_FarvaAuthor Commented:
I may not be calling this the right thing.  I am saying "scripts" but they are php web applications, over 200mb of folders and files with templates, etc, each with their own registration and login pages.  

So what do you need to edit?  The header files of both templates?  Login page of script 1?  Index.php of script 2?

Sorry again for such a basic question.  
0
Mohamed AbowardaSoftware EngineerCommented:
You only need to edit both PHP scripts that you perform login from.
0
Large_FarvaAuthor Commented:
Thanks for the clarification...here are the two login pages:

First login page:

 <script language="JavaScript" type="text/javascript" src="{checkout_url_link}template_js/type/login"></script>
       <div id="jroxContainer" class="jroxContainer">
	<div id="jroxTopBox" class="jroxTopBox">
 
        <div id="jroxLoginContent">
        
                 
        <div id="jroxMemberCreateAccountBox" class="jroxLoginBox">
			
            <form action="{submit_url}" method="post" name="jroxMemberLogin" id="jroxMemberLogin">
            
            <div id="jroxMemberLoginForm" class="jroxLoginForm">
            
            <div id="jroxMemberLoginHeading" class="jroxHeading">
            
            	
                
                <h2><span>{lang_member_login}</span></h2>
            
            </div>
			
            <div id="jroxMemberLoginEmail" class="jroxMemberLoginRow">
            	
                <label>{lang_email_address}</label><input name="username" type="text" class="generalInput required email"/>
			
            </div>
               
             <div id="jroxMemberLoginPass" class="jroxMemberLoginRow">
                 
                   <label>{lang_password}</label><input name="password" type="password" class="generalInput required"/> 
                <br/>
              </div>
            </div>
            
            <div id="jroxMemberLoginButton" class="jroxMemberLoginButton">
            	
            	<button name="login" type="submit" class="jroxButton" id="member_login_button" >{lang_member_login}</button>
                <button name="login" type="button" class="jroxButton" id="forget_password_button" onclick="window.location='{public_url_link}login/reset_password'">{lang_reset_password}</button>
            
            </div>
			
			<div class="clear"></div>
			
            </form>
            
         
            
           </div>

Open in new window


This is the second login:
<?php 
/**
 * [PHPFOX_HEADER]
 * 
 * @copyright		[PHPFOX_COPYRIGHT]
 * @author  		Raymond Benc
 * @package  		Module_User
 * @version 		$Id: login.html.php 2013 2010-11-01 13:12:53Z Raymond_Benc $
 */
 
defined('PHPFOX') or exit('NO DICE!'); 

?>
{$sCreateJs}
<div class="main_break">
	<form method="post" action="{url link="user.login"}" id="js_login_form" onsubmit="{$sGetJsForm}">
		<div class="table">
			<div class="table_left">
				<label for="login">{if Phpfox::getParam('user.login_type') == 'user_name'}{phrase var='user.user_name'}{elseif Phpfox::getParam('user.login_type') == 'email'}{phrase var='user.email'}{else}{phrase var='user.login'}{/if}:</label>
			</div>
			<div class="table_right">
				<input type="text" name="val[login]" id="login" value="" size="40" />
			</div>
			<div class="clear"></div>
		</div>
		
		<div class="table">
			<div class="table_left">
				<label for="password">{phrase var='user.password'}:</label>
			</div>
			<div class="table_right">
				<input type="password" name="val[password]" id="password" value="" size="40" />
			</div>
			<div class="clear"></div>
		</div>
		
		<div class="table_clear">
			<label><input type="checkbox" class="checkbox" name="val[remember_me]" value="" /> {phrase var='user.remember'}</label>
		</div>
		
		{plugin call='user.template_controller_login_end'}
		
		<div class="table_clear">
			<input type="submit" value="{phrase var='user.login_button'}" class="button" />{if Phpfox::getParam('user.allow_user_registration')} {phrase var='user.sign_for_site_name' url=$sSignUpPage name=$sSiteName}{/if}
			{plugin call='user.template_controller_login_block__end'}
		</div>
		
		<div class="table_clear">
			<a href="{url link='user.password.request'}">{phrase var='user.forgot_your_password'}</a>
		</div>	
	</form>	
</div>

Open in new window


They both have global headers I can put the session information in.  

Thanks again for the help!
0
Mohamed AbowardaSoftware EngineerCommented:
When the member login, you only need to store the login information in session variables.

See here:
http://www.php.net/manual/en/book.session.php
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.