My file layout is as follows:
Index.php: (URL = domain/index.php)

<?php
	session_start();
?>
<!-- BUNCH OF HTML -->
<?php
	if($_GET['page'] == 'portfolio')
		include('pages/portfolio.php');
?>
<!-- BUNCH OF HTML -->

Portfolio.php: (URL = domain/index.php?page=portfolio)

session_start();
if($_GET['view'] == 0){ // URL would be domain/index.php?page=portfolio&view=0
	$_SESSION['catImages'] = array();
	$_SESSION['catImages'][0] = "TEST";
} else { // URL would be domain/index.php?page=portfolio&view=#
	echo $_SESSION['catImages'][0];
}

In my script, I am including portfolio.php into index.php. When user is on domain/.../?view=0 .. it should create a new session array variable and set its first element to TEST. When the user is on any other page, domain/.../?view=# .. it should output the value.

For some reason, it acts like the session variable was not created.

My current output: (domain/index.php?page=portfolio&view=#)

ERROR: NULL VARIABLE

My expected output: (domain/index.php?page=portfolio&view=#)

TEST

And yes, obviously the user first goes to view=0 by default and then clicks on an item which takes him/her to view=# ... so it should create session variable at view=0 and carry it on to view=# but it doesn't do that.

Thank you for all the help in advance.

Recommended Answers

All 2 Replies

Okay on your index file you have used:


<?php
if($_GET == 'portfolio')
include('pages/portfolio.php');
?>

That should be:


<?php
if($_GET == 'portfolio'){
include('pages/portfolio.php');
}
?>

That might be the only problem :)
If you have headers already sent issues remove your session_start from portfolio and it should be fine ;)

For some reason, it acts like the session variable was not created.

My current output: (domain/index.php?page=portfolio&view=#)
Quote ...

ERROR: NULL VARIABLE

My expected output: (domain/index.php?page=portfolio&view=#)
Quote ...

TEST

First test your session is created or not..
like this:

session_start();
if($_GET['view'] == 0){ // URL would be domain/index.php?page=portfolio&view=0
	$_SESSION['catImages'] = array();
	$_SESSION['catImages'][0] = "TEST";
} else { // URL would be domain/index.php?page=portfolio&view=#
if(isset($_SESSION['catImages']) && !empty($_SESSION['catImages']))	
echo "Session not created";
else
echo $_SESSION['catImages'][0];
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.