Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

session - Stop direct access to .html pages without , redirect to login.php

I've been looking for an answer to make all my other .html pages in the root directory(or outside of) to be checked against if a session has taken place or not. If not direct visitor back to index.php (where login is, set in public_html). Does anyone have a work around for this? If so where would I put the "if no session redirect"? So if someone just goes directly to www.site.com/here.html the php runs and see's if the session had been instated, if not redirects to www.site.com/login.php.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use this:

<?php
if(!session_id()){
    header('Location: /index.php');
    exit;
}
?>

Just place it at the top of your html file

Or to be on the safe side, check if an index in the session is set like:

if(!isset($_SESSION['user'])){
    // redirect code
}

For this to work, on user login, you need to set

$_SESSION['user'] = 'something'; // a real value here!

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...