Jump to content

[SOLVED] redirect if no session


robert_gsfame

Recommended Posts

I have page1.php for users to login n if session is not empty then they can go to page2.php

 

Let say i type page2.php on url and as session is empty, it must redirect to page3.php

 

I have this for my code

if(empty($_SESSION['username'])){

print "<script>";

print " self.location='index.php';";

print "</script>"; }

 

but that code is not good enough as it will redirect to page2.php first before redirect to page3.php although the process is so fast

 

i wish to have it more professional look, can anyone help me?

Link to comment
Share on other sites

<?php
if (empty ($_SESSION['username']))
{ header ('Location: page3.php'); exit (0); }
?>

 

this has become a real guessing game.

 

what page is this code going to reside on?  page2.php?  and all you want it to do is redirect to page3.php?  the code above will work then.

Link to comment
Share on other sites

yeah i think header will be the answer but i always try to avoid using header as

i will always find this alert CANNOT MODIFY HEADER once using header()

 

What is the problem really?? should i put session_start after require_once or what??

 

there's nothing difficult in using headers, you just need to code it right.

 

post your code to get the fastest help.

Link to comment
Share on other sites

<?php session_start();?>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title></title>

<link rel="Shortcut Icon" href="images/icon3.jpg">

<style type="text/css">

<!--

#apDiv1 {

position:absolute;

left:27px;

top:10px;

width:962px;

height:699px;

z-index:1;

background-color: #FFFFFF;

}

</style>

</head>

<?php require_once('configuration.php'); ?>

 

<body onLoad=start() window.refresh bgcolor = "black">

<div id="apDiv1"></div>

  <?php

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

echo "<b>welcome</b>";

}else{

header("Location:index.php");}

 

 

Link to comment
Share on other sites

correct.  as that is considered (obviously) output.

 

also, change:

 

<?php session_start();?>

 

to:

 

<?php
session_start();
?>

 

as the whitespace between <?php[whitespace]session_start();?> can be considered output as well.

 

just move your block of code up to the top, before any output to the browser:

 

<?php
session_start();

if (!isset($_SESSION['uid']))
{ header("Location:index.php"); exit (0); }
?>

 

there is really no need to either say, "Welcome", or to redirect the user.  this is where better logic comes in to play, ultimately, making the usage of headers not so scary.

Link to comment
Share on other sites

<?php
session_start();
?>

 

as the whitespace between <?php[whitespace]session_start();?> can be considered output as well.

Nope, anything before the <?php can be considered output, inside the php tags is safe

ie this would be fine

<?php





header("Location: http://www.google.com");



?>

Link to comment
Share on other sites

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.