Jump to content

Recommended Posts

I'm trying to create a website so that if I go to http://websiteaddress.com/,  it will go to then index and if you are already logged in it will display the main page, but if you aren't logged in it'll show a login/registration page.

 

Also, how does the following work:

 

index.php?action=home

 

index.php?action=register

 

They show two seperate pages?

 

So, could it go to the home page if logged in, but if not logged in it'll go the registration/login page?

 

Link to comment
https://forums.phpfreaks.com/topic/251272-need-some-basic-help/
Share on other sites

And where are you stuck? Have you written any code for this yet?

 

No, I just want to understand how those two things would work.

 

Oh, I understand! I'm trying to make php templates.

 

I think I'm able to figure out something for what I'm trying to do. I'll just use php to verify I'm logged in using cookies and if I'm logged in it'll call up the mainpage template.

Use sessions.

At the top of every page you start the session and check for logged in user.

If are already logged in do nothing.

If not a logged in user can then redirect them to a registration page.

After registration redirect them back to either a login page, or the main page that contains a login.

 

 

Here is how the most basic of basic login scripts would look, using sessions:

 

We'll call this index.html:

<html>
<head><title>Test</title></head>
<body>
<form name="loginFrm" id="loginFrm" action="login.php" method="post">
Enter the secret code: <input type='password' name='code' id='code'> <input type='submit' name='send' id='send' value='Login'>
</form>
</body>
</html>

 

We'll call this login.php:

<?php
session_start();          //GOOD HABIT SAYS ALWAYS PUT THIS AT THE TOP, BEFORE ANYTHING ELSE

if (isset($_POST['password'])) {

     if ( strtolower( trim( stripslashes($_POST['password']) ) ) === 'test') {
          $_SESSION['login'] = 'John Doe User';
     }

     else {
          die(session_destroy());
     }

}

if (isset($_SESSION['login'])) {

     print_r($_SESSION['login']);

}

?>

 

If you type in 'test' (no quotes) as the secret code, you'll see 'John Doe User' from login.php, anything else and you won't see anything.

 

*NOTE*

The above html page and php script is a DEMO, to help you understand the basics. It is not suited to use in an actual production system, but will suffice for getting your head around the basics.

 

To answer your other question:

 

index.php?action=home

 

As far as PHP is concerned; the above is part of a url, everything to the right of '?' are key/value pairs in the GET namespace. So, if the page 'index.php' were reached with this URL, the array element $_GET['action'] would hold the value 'home'.

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.