And dont forget to regenerate session id in the future. There are bad guys out there.
Also unset_session after successful exit of account.

Huh? I don't understand?
What is wrong with my php?! Arrgghhh! Does this work for any of you?

What is wrong brother?

:)

I don't even have WAMP/LAMPP

So where do you run your PHP? Do you use Windows IIS?

TheSecOrg


Look i have been reading all your code and the problem is always have a session realm start on top of your file before any code. That includes html too.

session_start();

just declare a simple session start in its owm php tag up there. ON THE FIRST LINE WITHOUT A SPACE.

later you can set a session variable .

Also try not to use session variable raw like..

echo $_SESSION['username']

dont keep to that.

always put it in a standard variable. eg..

if($_POST){
$_SESSION['name']=$_POST['name'];
//dont forget to trim,strip_tags, etc.. clean always inputs that you receives even if it come from heaven. angels sometimes get lazy. trust no inputs.
}
// refer to session variable and put it in normal variable for use.
$foo=$_SESSION['name'];
echo $foo;

you get the idea?

:) Explore.

Do you run your php code on mac or unix...

There must be a platform...????

Please advice. ;)

Also try not to use session variable raw like..

echo $_SESSION['username']

dont keep to that.
always put it in a standard variable. eg..

A part from readability what is wrong with that?

evstevemd.

Apart from readabilty.... Speed.

Global variable scope is the last result to every pro. language engine. When you keep referring to globals so many times, you compensate on program execution time.

for examble

if(empty($_SESSION['name']) || $_SESSION['name']<3){

$_SESSION['name']= $foo;

}

You called 3 time a super global array. Not even a global variable. Its poor prog. practice.

Php deal with execution time and many programmers code fails to exe. due to this practices.

Assuming calling this 150 times on 400 lines of code. horror!

Explore :)

evstevemd.

Apart from readabilty.... Speed.

Global variable scope is the last result to every pro. language engine. When you keep referring to globals so many times, you compensate on program execution time.

for examble

if(empty($_SESSION['name']) || $_SESSION['name']<3){

$_SESSION['name']= $foo;

}

You called 3 time a super global array. Not even a global variable. Its poor prog. practice.

Php deal with execution time and many programmers code fails to exe. due to this practices.

Assuming calling this 150 times on 400 lines of code. horror!

Explore :)

Any benchmark to substantiate the claims? :)

I cannot see anything saying what you are saying.
Would you quote it? Anyone to comment on this?:-/

One thing you could do instead of using cookies is set it so the page you go to has the variable in the URL.

Example (not a valid link, in case anyone tries): http://daniweb.com/PHP2.php?age=9&someotherthing=1

So the code in PHP1.php would look something like this:

<?php
  $age = 9;
  $someotherthing= 1;

  echo "<a href=\"http://daniweb.com/PHP2.php?age=$age&someotherthing=$someotherthing\">Link to PHP2</a>";
?>

This way you don't need a form for the end user to fill out and you could just use whatever formula you were previously using to set the variable. If you don't want the end user to be able to see the variable and it's value or be able to manually change it then this is not advisable because anyone could just go to the address bar and change the value of each variable this way.

"I cannot see anything saying what you are saying.
Would you quote it? Anyone to comment on this?"

evstevemd,

Are you on php performance thing.???

The link i provided should at least give you the idea about how system engine works. This is the same as far as any prog. language is concern.

I am little busy but i am digging in my past achives to get you more info ok?
Stay with me.

:) Explore

Borzoi,

That is also another option. That is when he does not care about keeping the state but he demands something coming from a post array.

This mean inline get is certainly no what he needs.

Good idea lol :)

evstevemd,
Are you on php performance thing.???

I'm interested ;)

I use xampp or 000web host to use my php.
Do I put session_start() before <?php too?

Can you post your current code as well as errors you currently get?

I don't get any, I just don't get the variable.

[OFFTOPIC]

My apologies for taking this off topic for a second. But this comment struck my interest.

TheSecOrg
Also try not to use session variable raw like..

echo $_SESSION['username']

dont keep to that.

always put it in a standard variable. eg..

if($_POST){
$_SESSION['name']=$_POST['name'];
//dont forget to trim,strip_tags, etc.. clean always inputs that you receives even if it come from heaven. angels sometimes get lazy. trust no inputs.
}
// refer to session variable and put it in normal variable for use.
$foo=$_SESSION['name'];
echo $foo;

you get the idea?

:) Explore.

-evstevemd
-richieking

I was reading through this thread and saw this comment and having never heard this before had to do some research. Richieking is absolutely right in terms of setting a global to a local variable being faster to echo out. But, it is only nominally faster and I saw consistent results up over 100,000 echos.

I benchmarked this at 100 cycles of echoing a variable 1000 times:

First result is echoing the $_SESSION value directly.
Second result is echoing $var after $var = $_SESSION.
Third result was setting it to an array item instead of a variable. (Just out of curiosity)

Running Benchmark: Speed Between Accessing A $_SESSION Variable Or Local Rep Of A Session Variable
======================================================================
Running Test: test_Iterate_Session
	Cycles: 	 100
	Mean: 		 0.0004818988 secs.
	Median: 	 0.0004801750 secs.
	Mode: 		 0.0005009174 secs.
	Range: 		 0.0001130104 secs.
	Min: 		 0.0004498959 secs.
	Max: 		 0.0005629063 secs.
======================================================================
Running Test: test_Iterate_Local_Session_Rep
	Cycles: 	 100
	Mean: 		 0.0003332329 secs.
	Median: 	 0.0003280640 secs.
	Mode: 		 0.0003130436 secs.
	Range: 		 0.0000841618 secs.
	Min: 		 0.0003118515 secs.
	Max: 		 0.0003960133 secs.
======================================================================
Running Test: test_Iterate_Local_Session_Rep_Array
	Cycles: 	 100
	Mean: 		 0.0004192019 secs.
	Median: 	 0.0004239082 secs.
	Mode: 		 0.0004279613 secs.
	Range: 		 0.0001230240 secs.
	Min: 		 0.0003850460 secs.
	Max: 		 0.0005080700 secs.
======================================================================

About 1.5x faster local vs $_SESSION

100 cycles 100000 echos

Running Benchmark: Speed Between Accessing A $_SESSION Variable Or Local Rep Of A Session Variable
======================================================================
Running Test: test_Iterate_Session
	Cycles: 	 100
	Mean: 		 0.0448146057 secs.
	Median: 	 0.0447440147 secs.
	Mode: 		 0.0447440147 secs.
	Range: 		 0.0043771267 secs.
	Min: 		 0.0440149307 secs.
	Max: 		 0.0483920574 secs.
======================================================================
Running Test: test_Iterate_Local_Session_Rep
	Cycles: 	 100
	Mean: 		 0.0297587132 secs.
	Median: 	 0.0297429562 secs.
	Mode: 		 0.0298109055 secs.
	Range: 		 0.0044479370 secs.
	Min: 		 0.0290210247 secs.
	Max: 		 0.0334689617 secs.
======================================================================
Running Test: test_Iterate_Local_Session_Rep_Array
	Cycles: 	 100
	Mean: 		 0.0376270676 secs.
	Median: 	 0.0375969410 secs.
	Mode: 		 0.0375020504 secs.
	Range: 		 0.0036828518 secs.
	Min: 		 0.0367841721 secs.
	Max: 		 0.0404670238 secs.
======================================================================

Again approx. 1.5x faster.

While it IS faster, its only marginally faster at best.
As always code is available if anyone is interested enough to check out.
[/OFFTOPIC]

it has always been the case.

locals are always faster. :)

thanks buddy ;)

So what's wrong with my code?

look i tried to help you, the best you did was to flag me bad post because of basic platform i asked you.

mmm ;)

look i tried to help you, the best you did was to flag me bad post because of basic platform i asked you.

mmm ;)

May be he did it accidentally!

Post your current code and say what you expect it to do :)

[OFFTOPIC]

My apologies for taking this off topic for a second. But this comment struck my interest.

-evstevemd
-richieking

I was reading through this thread and saw this comment and having never heard this before had to do some research. Richieking is absolutely right in terms of setting a global to a local variable being faster to echo out. But, it is only nominally faster and I saw consistent results up over 100,000 echos.

I benchmarked this at 100 cycles of echoing a variable 1000 times:

First result is echoing the $_SESSION value directly.
Second result is echoing $var after $var = $_SESSION.
Third result was setting it to an array item instead of a variable. (Just out of curiosity)

Running Benchmark: Speed Between Accessing A $_SESSION Variable Or Local Rep Of A Session Variable
======================================================================
Running Test: test_Iterate_Session
	Cycles: 	 100
	Mean: 		 0.0004818988 secs.
	Median: 	 0.0004801750 secs.
	Mode: 		 0.0005009174 secs.
	Range: 		 0.0001130104 secs.
	Min: 		 0.0004498959 secs.
	Max: 		 0.0005629063 secs.
======================================================================
Running Test: test_Iterate_Local_Session_Rep
	Cycles: 	 100
	Mean: 		 0.0003332329 secs.
	Median: 	 0.0003280640 secs.
	Mode: 		 0.0003130436 secs.
	Range: 		 0.0000841618 secs.
	Min: 		 0.0003118515 secs.
	Max: 		 0.0003960133 secs.
======================================================================
Running Test: test_Iterate_Local_Session_Rep_Array
	Cycles: 	 100
	Mean: 		 0.0004192019 secs.
	Median: 	 0.0004239082 secs.
	Mode: 		 0.0004279613 secs.
	Range: 		 0.0001230240 secs.
	Min: 		 0.0003850460 secs.
	Max: 		 0.0005080700 secs.
======================================================================

About 1.5x faster local vs $_SESSION

100 cycles 100000 echos

Running Benchmark: Speed Between Accessing A $_SESSION Variable Or Local Rep Of A Session Variable
======================================================================
Running Test: test_Iterate_Session
	Cycles: 	 100
	Mean: 		 0.0448146057 secs.
	Median: 	 0.0447440147 secs.
	Mode: 		 0.0447440147 secs.
	Range: 		 0.0043771267 secs.
	Min: 		 0.0440149307 secs.
	Max: 		 0.0483920574 secs.
======================================================================
Running Test: test_Iterate_Local_Session_Rep
	Cycles: 	 100
	Mean: 		 0.0297587132 secs.
	Median: 	 0.0297429562 secs.
	Mode: 		 0.0298109055 secs.
	Range: 		 0.0044479370 secs.
	Min: 		 0.0290210247 secs.
	Max: 		 0.0334689617 secs.
======================================================================
Running Test: test_Iterate_Local_Session_Rep_Array
	Cycles: 	 100
	Mean: 		 0.0376270676 secs.
	Median: 	 0.0375969410 secs.
	Mode: 		 0.0375020504 secs.
	Range: 		 0.0036828518 secs.
	Min: 		 0.0367841721 secs.
	Max: 		 0.0404670238 secs.
======================================================================

Again approx. 1.5x faster.

While it IS faster, its only marginally faster at best.
As always code is available if anyone is interested enough to check out.
[/OFFTOPIC]

I think it makes difference for sites with massive user.
Thanks for proving the point. I will adjust my practices to accommodate that

I wouldn't put to much focus on it, you'd have to be echoing millions of session variables for it to even begin to make an impact. Spend the time optimizing the more bottleneck prone areas of your code, you'll see a much bigger return.

3 Hours Ago
This post is useful and well-written 0 This post is unclear
Re: PHP 'Public' variables?
So what's wrong with my code?

TheSecOrg post your code again if any. Just cant go back on that.

Roll that sir please. ;)

I wouldn't put to much focus on it, you'd have to be echoing millions of session variables for it to even begin to make an impact. Spend the time optimizing the more bottleneck prone areas of your code, you'll see a much bigger return.

You are right.
I was saying about "best practices" ;)

look i tried to help you, the best you did was to flag me bad post because of basic platform i asked you.

mmm ;)

?! I didnt flag your post as bad! I'm sorry but I really didnt! I pressed Post Reply then my browser said I wasn't connected to the internet =.=, I really didnt flag your post but if I did for whatever reason I didnt mean it!

I just did a simple

<?php
session_start();
$_SESSION['greeting'] = "Hello world";
echo $_SESSION['greeting'];
echo '<a href="PHP2.php">Next</a>';
<?

and in PHP2

<?php
session_start();
echo $_SESSION['greeting'];
<?

and that didnt even work...

use this old global array

$HTTP_SESSION_VARS['foo']

This is the old php session array.
This will help you ;)

Thanks, how do I do use this?
Do I just start the session make an array and make $Var=$HTTP_SESSION_VAR?

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.