Jump to content

Problem with $_SESSION!


pappakaka

Recommended Posts

I have a problem, you can see it on this page and then by following the steps below: http://www.randommovietitle.com/Testing/

 

1. Try clicking the random button. You will come to a random movie.

 

2. Go back and and try to choose some options like 2D Animation as genre, between the years 2000-2011. You will get a random movie that fits you options. Also, the query for the options you choose is echo'd out on in the corner aswell as the year span above the logo.

 

3. After you've done this. Close the tab/page and go to this URL: http://www.randommovietitle.com/Testing/movies.php?mid=100. You will come to the movie Crank. But do you see up in the corner and above the logo? The query and year span is still there and when you click the "Randomize again" button on that page, you will come to a new movie BUT only a movie that fits the options you choose in step 2.

 

 

 

I know that this is happening because of the use of $_SESSION. But what I can't figure out is how to make it stop when you leave the movies.php page. Or even better, by using something else than $_SESSION entirely? For example, if I was on this site a few minutes ago and choose a few options that was saved in the $_SESSION veriable. Then come back and want to search for a completely random movie without any options. The options I choose a few minutes ago will still be in the $_SESSION variable and used in the search.

 

How can I change this?

 

Here is the form on the first page and the code I use on movies.php:

<form class="main_form" action="movies.php" method="post" name="form1">
		<input class="random_btn" name="next_btn" value="Random" type="submit" onclick="return validate()" /><br />

	<div class="index_bgbox">

  			<label class="genre_label" value="genre">Genre</label><br />
        	<select multiple="multiple" name="genre[]" id="genre" size="7">
            	<option value="01">2D Animation</option>
                <option value="02">3D Animation</option>
                <option value="03">Action</option>
                <option value="04">Adventure</option>
                <option value="05">Animals</option>
                <option value="06">Comedy</option>
                <option value="07">Comics Adaptation</option>
                <option value="08">Crime</option>
                <option value="09">Dance</option>
                <option value="10">Disaster</option>
                <option value="11">Documentary</option>
                <option value="12">Drama</option>
                <option value="13">Family</option>
                <option value="14">Fantasy</option>
                <option value="15">Fighting</option>
                <option value="16">Game Adaptation</option>
                <option value="17">History</option>
                <option value="18">Horror</option>
                <option value="19">Thriller</option>
                <option value="20">Romance</option>
                <option value="21">Music</option>
                <option value="22">Mystery</option>
                <option value="23">Parody</option>
                <option value="24">Psychological</option>
                <option value="25">Sci-Fi</option>
                <option value="26">Sport</option>
                <option value="27">Teen</option>
                <option value="28">War</option>
                <option value="29">Western</option>
            </select>
            
            <label class="year_label" value="YearForm">Year</label><br />
            <select class="year_from" name="YearFrom">
          	<option value="">Select From</option>
          	<option value="1930">1930</option>
          	<option value="1931">1931</option>
          	<option value="1932">1932</option>
          	<option value="1933">1933</option>
          	...etc...
             
            </select>
          <select class="year_to" name="YearTo">
          	<option value="">Select To</option>
          	<option value="1930">1930</option>
          	<option value="1931">1931</option>
          	<option value="1932">1932</option>
          	<option value="1933">1933</option>
          	...etc...

          </select>
          </div>     
</form>

 

<?php 
session_start();

require("Includes/connection.php");
$connection = @mysql_connect($ControlHost, $ControlUser, $ControlPass) 	or die("Couldn't connect to server");
$db = @mysql_select_db("$ControlDb", $connection) 	or die("Couldn't select database");

if (isset($_GET['mid'])) {
echo $_SESSION['genre_string'];
	$mid=(int)$_GET['mid'];
	$result = mysql_query("SELECT * FROM movie WHERE MID='$mid'");	
	$row = mysql_fetch_array($result);
	$MID = $row["MID"];
	$Title = $row["Title"];
	$YearFrom = $row["YearFrom"];
	$RMTRating = $row["RMTRating"];
	$IMDBRating = $row["IMDBRating"];
	$Description = $row["Description"];
	$Genree = $row["Genree"];
	$Genree = explode(",", $Genree);
	$Poster = $row["Poster"];
	$TrailerLink = $row["TrailerLink"];
	$MoreInfo = $row["MoreInfo"];
	$Prequel = $row["Prequel"];
	$Sequel = $row["Sequel"];
	}
elseif (!isset($_GET['mid'])) {


$genre_string="";
//$genre_string1="";
if(empty($_REQUEST['genreString1'])){
	if(!empty($_REQUEST['genre'])){
		$genre=$_REQUEST['genre'];
		$total_genre=count($genre);

		$genre_string1="";
		for($i=0; $i<$total_genre;$i++){
			$genre_string .= "Genree LIKE '%" . $genre[$i] . '%' . "'". " AND ";
			$genre_string1 .= $genre[$i] . " OR ";
		}
		$genre_string = substr($genre_string, 0, -4);
		$genre_string1 = substr($genre_string1, 0, -4);
		$genre_string = "WHERE" . "(" . $genre_string . ")";
	}
	if( !empty($_REQUEST['YearFrom']) ){
		$YearFrom = $_REQUEST['YearFrom'];
		$YearTo = $_REQUEST['YearTo'];
		if(!empty($_REQUEST['genre'])) $genre_string .= " AND "; else $genre_string .= "Where ";
		$genre_string .= "(YearFrom BETWEEN $YearFrom AND $YearTo)";
	}
}elseif(!empty($_REQUEST['genreString1'])){
	$genre_string1 = $_REQUEST['genreString1'];
	$pieces = explode(" OR ", $genre_string1);
	for($ii=0; $ii<count($pieces);$ii++){
		$genre_string .= "Genree LIKE '%" . trim($pieces[$ii]) . '%' . "'" . " AND ";
		$genre[]=trim($pieces[$ii]);
	}
	$genre_string = substr($genre_string, 0, -4);
	if( !is_numeric(trim($pieces[0]))) $genre_string = ""; else $genre_string = "WHERE" . "(" . $genre_string . ")";
	if( !empty($_REQUEST['YearFrom']) ){echo "111";
		$YearFrom = $_REQUEST['YearFrom'];
		$YearTo = $_REQUEST['YearTo'];
		if( is_numeric(trim($pieces[0]))) $genre_string .= " AND (YearFrom BETWEEN $YearFrom AND $YearTo)";
		if( !is_numeric(trim($pieces[0]))) $genre_string = " Where (YearFrom BETWEEN $YearFrom AND $YearTo)";
	}

}
	if (!empty($genre_string)) {
		$_SESSION['genre_string'] = $genre_string;
		$YearFrom = $_REQUEST['YearFrom'];
		$YearTo = $_REQUEST['YearTo'];
		$_SESSION['YearFrom'] = $YearFrom;
		$_SESSION['YearTo'] = $YearTo;
	}
	else {
		$genre_string = $_SESSION['genre_string'];
	}

	$result = mysql_query("SELECT * FROM movie $genre_string ORDER BY RAND() LIMIT 0,1");
	$row = mysql_fetch_array($result);
	$MID = $row["MID"];

	header( "Location: movies.php?mid=" . $MID ) ;
}

?>

Link to comment
https://forums.phpfreaks.com/topic/242583-problem-with-_session/
Share on other sites

Session cookies are set to expire when the browser closes. There's no way to delete them when you close the tab or window, at least as far as I know. The only thing I can think of is, if the user visits a movie that doesn't match their option for random genre or year, clear those settings so random is completely random again. Or track the last time your visitor visited a movie and ignore session options after a few minutes. But all of those are kind of clumsy.

 

Edit: what if you pass the genre and year data with $_GET instead?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.