How can I use PHP to create login-controlled web pages?

Recommended Answers

All 4 Replies

there are many tutorials and readily available scripts out there.

There is one here. It is an old one, but it should be able to do the job. I was planning to make an update, but the original owner won't repond to my requests.

Controlling access to pages requires sessions and access control. php sessions are used to store data per user. Access control is how php uses session to access data.. so..

Here is how a simple login controlled page would be.

Content Protected Page(this is placed in the header of the files needed to be protected.)

<?php 


    session_start();

    if($_SESSION['access_granted'] == 'no'){
        session_destroy();
        header("Location: " . $_SERVER['HTTP_REFERER']);
        exit;
    }else{

    //Show all content that is protected 

    }

?>

This checks if a session is started with the var access_granted not equal to no. If the access_granted var is set to no the session will be destroyed and the user will be redirected to where they came from. if the access_granted var is set to anything else, the page will load.

Member Avatar for diafol

The OP has been banned and has a history of asking daft, flowery questions. I doubt very much whether she(he?) needed this in any case. However, if you think others may benefit from your advice, please carry on. Be aware that this topic has been discussed to death, so more likely your finely crafted solution may already be present in different forms a hundred times over. Vague posts will draw downvotes.

well done gabrielcastillo
your code is working properly thanks..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.