1

when I am redirecting with "a href .." to another page I lose all my SESSION variables. So I want to redirect only by using header() php function. How I can create a link that when the user use it redirects him to another page using header()? Thank you

here is my code: source page:

session_save_path("myrootdir/cgi-bin/tmp");
session_start();
....
$id = $_SESSION['id']; //session variable passed succesfully from log in page
$usr = $_SESSION['usr'];//by using header('sourcepage.php');
....
< a href='http://targetpage.php'>target< / a>

target page:

session_save_path("myrootdir/cgi-bin/tmp");
session_start();
...
if(isset($_SESSION['id']) && isset($_SESSION['usr'])) {
   echo "success";
}
else{
   echo "failed to pass";
}

I always get failed to pass!

UPDATE

now I have another strange issue...

$_SESSION['id']=$_POST['id'];
$_SESSION['usr']=$_POST['usr'];

if(isset($_SESSION['id']) && isset($_SESSION['usr'])) {
    $id = $_SESSION['id'];
    $usr = $_SESSION['usr'];
    $qry = mysql_query("SELECT * FROM members WHERE id = '". mysql_real_escape_string( $id ) ."' AND usr = '". mysql_real_escape_string( $usr ) ."'");
    echo "Welcome ".$usr;
    echo "Session id".session_id();
    if(mysql_num_rows($qry) != 1) { Destroy(); }
  } else { Destroy(); }

when I check if someone is logged in like this it works perfect in every page but when I remove the echo lines then it calls Destroy() function.

7
  • using header redirection I dont have any problem. By using html href redirection I lose all my SESSION vars. You mean I have to call exit() inside href ? Commented Aug 1, 2013 at 11:59
  • I use session_start on every page Commented Aug 1, 2013 at 12:45
  • ok i edited the question with the code Commented Aug 1, 2013 at 13:15
  • Where is header() function call? Commented Aug 1, 2013 at 13:48
  • the header('sourcepage.php') is in the log in page which redirects to the sourcepage.php with the session variables succesfully Commented Aug 1, 2013 at 13:51

4 Answers 4

1

This method works only if you call "header" before any output to browser, including tags. So I supose it not the best idea to use "header".

If you losing SESSION variables, try to add some JavaScript that will create form whith vars you need inside inputs, and submit it to new page.

Is this redirection inside one domain?

UPDATE:

php code on current page:

echo '<input type="text" id="sessionID" style="display: none;" value="'.$_SESSION['id'].'">';
echo '<input type="text" id="sessionUri" style="display: none;" value="'.$_SESSION['usr'].'">';

JS code:

< a onclick="sendSessionVars()">target< / a>

<script>
    function sendSessionVars(){
        var body=document.getElementsByTagName('body')[0];
        var sessionId=document.getElementById('sessionID').value;
        var sessionUri=document.getElementById('sessionUri').value;
        var form=document.createElement('form');
        form.setAttribute('method','post');
        form.setAttribute('style','display:none');
        form.setAttribute('action','targetpage.php');
        body.appendChild(form);
        var id=document.createElement('input');
        id.setAttribute('type','hidden');
        id.setAttribute('name','id');
        id.setAttribute('value',sessionId);
        var usr=document.createElement('input');
        usr.setAttribute('type','hidden');
        usr.setAttribute('name','usr');
        usr.setAttribute('value',sessionUri);
        form.appendChild(id);
        form.appendChild(usr);
        form.submit();  
    }
</script>

on jQuery would be much less code, but it will work slower.

and php code on target page:

$_SESSION['id']=$_POST['id'];
$_SESSION['usr']=$_POST['usr'];

You can also post it to some vars. It's pretty straightforward way.

But you should not lose SESSION variables. Check is your session_start(); is ok.

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

2 Comments

yes its inside the same domain. I use < a href='mysite.com/page1.php > page1 < /a > . Can you give me an example in javascript to pass the $_SESSION['usr'] and $_SESSION['id'] vars to the new page?
it worked! thank you! but I have a strange issue now! please check update in main question
1

header("Location: yourpage.php") try this

1 Comment

I want to have a link on my page so when the user clicks it the redirects him using header()
0

In php use it header('Location:page_name.php') But I prefer to use javascript cause you might headers problem using it alot So here is my javascript

       echo "<script>window.location='page.php'</script>";

Comments

0

use header("Location:some.php")

and putting header redirect before html tag is a good practice. You might want to reconstruct your website structure, if you find that it is below the html tag.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.