2

I have two files; the logic is such that if session is started i've to run a piece of code and if its not set then i have to run the else part. In any case i need to throw the page onto itself. Here's my first file


First File (file1.php)

<?php require_once('file2.php'); if(isset($_SESSION)) echo '<br>Session set!'; else { //echo '<br>Session NOT set...'; sessStart(); header("Location:file1.php"); } ?> 


Second File (file2.php)

<?php function sessStart() { session_start(); //some other code here but nothing that echo's } ?> 

What I do here is call sessStart() method to initialize the session in the else part the first time it is run. The next time it should not go into the if section but else part.

Somehow the code doesn't redirect the file to the same file and the part where the session is set doesn't initialized. If i store something in the session in 2nd file i can retrive it in the first file so the session is started successfully but how to redirect?

What's wrong that i am doing?

7
  • wait you are attempting to redirect to file1.php which is itself. so nothing is redirected Commented Feb 13, 2011 at 22:23
  • Humm so what i must do to get this done? Commented Feb 13, 2011 at 22:26
  • why not just put session_start(); on top of file1.php and forget about redirecting Commented Feb 13, 2011 at 22:29
  • 2
    Is the question "How to redirect a page to the same page in php" or "how to get a session working"? Commented Feb 13, 2011 at 22:33
  • do u have ob_start ob_flush in the script? Commented Feb 13, 2011 at 22:39

3 Answers 3

3

Ok firstly its pointless what your doing.

kjy112's answer does absolutely nothing apart from cause the page to constantly refresh.

you should never need to do a check on _session, you just need to make sure that before any usage of $_SESSION or any content sent out you call session_start().

I would recommend that you restructure your current code into something a little more like this.

require_once "includes/startup.php"; 

and within startup.php

//Load primary includes require_once "libraries/session.php"; require_once "libraries/input.php"; require_once "libraries/output.php"; //etc if(!session_id()) { session_start(); session_regenerate_id(); //Other session bits } 

That's basically all you need you do.

Sign up to request clarification or add additional context in comments.

6 Comments

uff php ya it cache'd something now its refreshing :/ I'd try other methods.
yep it basically is constant refresh.
Please re-explain what you mean by uff php ya it cache'd
Error on my part restarted IIS and it started behaving exactly like you said
I know what you trying to do now...use socket for chat application.
|
1

I was trying to do something like below code but use of session in my example made you guys think am messing up session initialization :) It was more of a header problem. The below code is NOT POSSIBLE

//filename [1.php] <?php $x = 'n'; if($x == 'y') echo 'entered top'; else { $x = 'y'; header('1.php'); } ?> 

You cannot redirect a page to itself using the header(); it will constantly get redirected.

Comments

0

You can't do the header redirect after you have echo'd anything.

Your example would work if you didn't echo "not set" before you redirected. When did you expect to see this message if redirecting immediately after it.

Personally I wouldn't put the session start functionality in a function, I would include a file that ran session_start() in every script you need it in.

2 Comments

For testing purpose i disabled all echo's still its not working.
Yes it gets 'This webpage has a redirect loop' in chrome.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.