...

View Full Version : Change variable in session and redirect user



santino_london
12-17-2010, 11:11 AM
Hello There,

I've set-up a web site but I absolutely need a disclaimer to be agreed by the visitor when he open the web page.

I've got an structure with php include and everything work great.

I've added a session to store a variable to know if the user has or not agreed the disclaimer:



session_start();
$_SESSION['Disclaimer'] = 0; // store session data


To check if the disclaimer must be display:



if($_SESSION['Disclaimer'] == 0)
$page = $page_disclaimer;


This is in my Index php which manage the include.

So now I've set-up a page "Disclaimer" to be include in the page index.php

My problem is I want via a form in the page Disclaimer.php change the variable $_SESSION['Disclaimer'] to 1 and redirect to home page.

Here is the code which is not working, I'm not even sure the variable is properly change to 1 and it keep display the disclaimer:



<form method="LINK" action="<?PHP $_SESSION['Disclamer'] = 1;
echo("index.php?page=Home");
?>">
<input type="submit" value="I agree" />
</form>


I've tried many method but no correct result....I'm not a php dev so any help will be welcome.

Tks

abduraooft
12-17-2010, 11:19 AM
Change
<form method="LINK" action="<?PHP $_SESSION['Disclamer'] = 1;
echo("index.php?page=Home");
?>"> to

<form method="GET" action="index.php?page=Home&amp;unset"> and at the top of "Home" page, add the following code

if(isset($_GET['unset']))
$_SESSION['Disclamer'] = 1
(assuming, all you pages have a call to session_start() at the top )

santino_london
12-17-2010, 12:44 PM
Tks for the reply.

Ok I tried but I probably miss something.

I'm starting only one session (as I assume otherwise you have multiple session??) in my Index page which include the other page like Home, Disclaimer, about,...

so I've added in he php of the index page this section:


if(isset($_GET['unset']))
$_SESSION['Disclamer'] = 1 ;

if($_SESSION['Disclaimer'] == 0)
$page = $page_disclaimer;

my form in the page "Disclaimer" itself include in the index page:


<form method="GET" action="index.php?page=Home&amp;unset">
<input type="submit" value="I agree" />
</form>

And I just start one session at the beginning of my php which manage the include in the Index page:


<?php
session_start();
$_SESSION['Disclaimer'] = 0; // store session data


But still a disclaimer include all the time after I click on Agree :confused:

santino_london
12-17-2010, 06:27 PM
Anyone to suggest a solution? :thumbsup:

Tks

adarshakb
12-17-2010, 07:36 PM
I'm starting only one session (as I assume otherwise you have multiple session??) in my Index page which include the other page like Home, Disclaimer, about,...

At the start of every page BEFORE ANY SPACE OR ANY CODE other than <?php start session



<?php
session_start();
//rest of the code


Also do what abduraooft said in early post after that

Also you may have to do a value check if you have a "disagree" button also ... by using the corresponding $_POST[] or $_GET[] variables

And dont start sessions once its started later in the SAME page.... (in another page you HAVE to start)



<?php
session_start();
if(isset($_GET['submit_button']) && $_GET['submit_button']=="I agree")
{
//redirect or set the page here
}
?>

<form method="get" action="YOUR_HOME.php">
<input name="submit_button" type="submit" value="I agree" />
</form>

santino_london
12-18-2010, 10:00 AM
At the start of every page BEFORE ANY SPACE OR ANY CODE other than <?php start session



<?php
session_start();
//rest of the code




Tks for helping!

I've already try to start a session at the beginning of each page but it generate the error below when it include Disclaimer.php:


Notice: A session had already been started - ignoring session_start()

So I can only start one session in the index page.

Just to summarize, here what I've got:

Page Disclaimer.php (which is include in index.php)



<body>
<form method="GET" action="index.php?page=Home&amp;unset">
<input type="submit" value="I agree" />
</form>
</body>


Home.php (I start a session but it generate an error as well)



<body>
<?php
session_start();
if(isset($_GET['unset']))
$_SESSION['Disclaimer'] = 1
?>



and index.php at the beginning of the page:



<body>
<?php
session_start();
?>


and in html section I include the other pages:



<?php
$_SESSION['Disclaimer'] = 0; // store session data

//code which manage the include

if($_SESSION['Disclaimer'] == 0)
$page = $page_disclaimer;


So at the moment, I can see first the Disclaimer but impossible to change the variable $_SESSION['Disclaimer'] to 1 so the visitor can see any other pages.

abduraooft
12-18-2010, 10:19 AM
So I can only start one session in the index page.
That's what you actually need.

santino_london
12-18-2010, 10:23 AM
That's what you actually need.

Ok so any idea why it do not work? What I do wrong?

abduraooft
12-18-2010, 03:19 PM
and in html section I include the other pages: What do you mean by that? Have you written the php code in an .html file?

Anyway, to "use" session, you need to have a call to the function session_start() at the top of all pages.

adarshakb
12-19-2010, 08:32 PM
At the start of every page BEFORE ANY SPACE OR ANY CODE other than <?php start session...
you may have misunderstood me...

You have to start it at top of ALL pages.

But say you have a include() in the page... that page shouldnt start a session again.

Thus only 1 session is started

adarshakb
12-19-2010, 08:36 PM
Home.php (I start a session but it generate an error as well)



<body>
<?php
session_start();
if(isset($_GET['unset']))
$_SESSION['Disclaimer'] = 1
?>



and index.php at the beginning of the page:



<body>
<?php
session_start();
?>



You are starting the output BEFORE you start the session
It should come BEFORE all other things.
So <body> THEN session start is wrong.....

Put this at the TOP <?php session_start() ?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum