robert_gsfame Posted November 17, 2009 Share Posted November 17, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/181872-solved-redirect-if-no-session/ Share on other sites More sharing options...
JonnoTheDev Posted November 17, 2009 Share Posted November 17, 2009 proper redirect <?php if(!strlen(trim($_SESSION['username']))) { header("Location:index.php"); exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/181872-solved-redirect-if-no-session/#findComment-959172 Share on other sites More sharing options...
jjacquay712 Posted November 17, 2009 Share Posted November 17, 2009 Use the header function: <?php if ( $_SESSION['username'] ) { header("Location: page2.php"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/181872-solved-redirect-if-no-session/#findComment-959173 Share on other sites More sharing options...
robert_gsfame Posted November 17, 2009 Author Share Posted November 17, 2009 it will result the same as when i use <self.location> Any other method??? Quote Link to comment https://forums.phpfreaks.com/topic/181872-solved-redirect-if-no-session/#findComment-959180 Share on other sites More sharing options...
robert_gsfame Posted November 17, 2009 Author Share Posted November 17, 2009 Cannot modify header information - headers already sent by Quote Link to comment https://forums.phpfreaks.com/topic/181872-solved-redirect-if-no-session/#findComment-959183 Share on other sites More sharing options...
mrMarcus Posted November 17, 2009 Share Posted November 17, 2009 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/181872-solved-redirect-if-no-session/#findComment-959185 Share on other sites More sharing options...
mrMarcus Posted November 17, 2009 Share Posted November 17, 2009 Cannot modify header information - headers already sent by k, stop for a minute. you cannot have output to the browser before sending headers. post page2.php here so we can see what is being outputted to the browser. Quote Link to comment https://forums.phpfreaks.com/topic/181872-solved-redirect-if-no-session/#findComment-959187 Share on other sites More sharing options...
robert_gsfame Posted November 17, 2009 Author Share Posted November 17, 2009 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?? Quote Link to comment https://forums.phpfreaks.com/topic/181872-solved-redirect-if-no-session/#findComment-959189 Share on other sites More sharing options...
mrMarcus Posted November 17, 2009 Share Posted November 17, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/181872-solved-redirect-if-no-session/#findComment-959193 Share on other sites More sharing options...
robert_gsfame Posted November 17, 2009 Author Share Posted November 17, 2009 <?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");} Quote Link to comment https://forums.phpfreaks.com/topic/181872-solved-redirect-if-no-session/#findComment-959194 Share on other sites More sharing options...
robert_gsfame Posted November 17, 2009 Author Share Posted November 17, 2009 i think i get alert CANNOT MODIFY as i put the header on the body <html> is that right?? Quote Link to comment https://forums.phpfreaks.com/topic/181872-solved-redirect-if-no-session/#findComment-959201 Share on other sites More sharing options...
MadTechie Posted November 17, 2009 Share Posted November 17, 2009 correct, at the top should be <?php session_start(); if (!isset($_SESSION['uid'])){ header("Location:index.php"); exit(); } ?> and <?php if (isset($_SESSION['uid'])){ echo "<b>welcome</b>"; }else{ header("Location:index.php");} should be <b>welcome</b> Quote Link to comment https://forums.phpfreaks.com/topic/181872-solved-redirect-if-no-session/#findComment-959202 Share on other sites More sharing options...
mrMarcus Posted November 17, 2009 Share Posted November 17, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/181872-solved-redirect-if-no-session/#findComment-959203 Share on other sites More sharing options...
robert_gsfame Posted November 17, 2009 Author Share Posted November 17, 2009 thanx mrmarcus!! Quote Link to comment https://forums.phpfreaks.com/topic/181872-solved-redirect-if-no-session/#findComment-959204 Share on other sites More sharing options...
MadTechie Posted November 17, 2009 Share Posted November 17, 2009 <?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"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/181872-solved-redirect-if-no-session/#findComment-959207 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.