Session_id changing in a page

<?php session_id('hello1'); session_start(); ?>
The code above will change “session_id”.
I like to change session_id after reading the session_id is given.

The code below doesn’t work correctly, but I hope it shows what I want.

[code]<?php
session_start();
sessionid = session_id();

echo $sessionid;

session_id(‘hello2’);
session_start();
sessionid = session_id();
echo $sesssionid;
?> [/code]

The code below can regenerate the session_id but I cannot put my own value(“hello2”) to the session_id

session_start();

$old_sessionid = session_id();

session_regenerate_id();

$new_sessionid = session_id();

echo "Old Session: $old_sessionid[br /]";
echo "New Session: $new_sessionid[br /]";

How can I regenerate session_id with my own value in a page?

Read all about how to use sessions.

Try this:

<?php 
error_reporting(-1);
ini_set('display_errors', true);

session_start();
$_SESSION['sessionid']  = session_id(); 
$_SESSION['hello2']     = 'Hello World';
$_SESSION['today']      = date('l)'   // (lowercase 'L') shows full text day


echo '<pre>'; // cosmetic line-spacing
  print_r( $_SESSION );
echo '</pre>';

?>
1 Like

Thank you, John_Betong. for your code, but I don’t quite understand your code although I test it and I read it as you recommend “how to use sessions”.
I felt I don’t unserstand fully although I read it.
At the moment I like to say what I want more exactly.

I like to change session_id() instead of $_SESSION[''each session"]

in other words, I like to use my own session value for session_id

if I use session_id(‘my own session value’) before session_start like the code below, the session_id value is made by me.

[code]

<?php session_id('hello1'); session_start(); ?>

[/code].but the code above works in the very first part of a web page

I like to make another session_id in the middle of a page after the old session(‘old session_id"’) reading for getting some session values by using $_SESSION[''each session"].

For that I can use session_regenerate_id();

But I like to use my own value(‘hello2’) for session_regenerate_id().
The code below is all code at http://dot.kr/x-test/php/session/sessID/regenerate2.php

<?php session_id('hello1'); session_start(); $old_sessionid = session_id(); session_regenerate_id('hello2'); $new_sessionid = session_id(); echo "Old Session: $old_sessionid<br />"; echo "New Session: $new_sessionid<br />"; ?>The code above doesn’t work as my expectation but it shows what I want. I like to make the value of the new session_id as “hello2” instead of the hash value like “f2d98b2e424312e913b238b345116da9”.

Maybe easier to understand if it was in Korean?

http://php.net/manual/kr/book.session.php

Notice that
string session_id ([ string $id ] )
takes string.

But
bool session_regenerate_id ([ bool $delete_old_session = false ] )
takes a boolean.

session_id ([ string $id ] ); syntax error, unexpected ‘[’, expecting ‘)’

session_id (‘string’, [ $id ] ); syntax error, unexpected ‘[’

session_id (‘string’, $id ); Wrong parameter count for session_id()

session_id ($id ); no error, so it regenerate new session_id, but the new session_id is hash value instead of $ID

bool session_regenerate_id ([ bool $delete_old_session = false ] ); syntax error, unexpected T_STRING

That isn’t actual code.

That’s the way PHP indicates the syntax.
eg.

return_type function_name( argument_type $variable, [ optional_argument_type $variable = default_value ])

Not all have optional arguments or defaults, but that’s how they are when a function does have them.

Most if not all documentation pages have Examples and User Contributed Notes that are worth looking at too.

So
string session_id ([ string $id ] )
the function returns a string
the function takes an optional string parameter

bool session_regenerate_id ([ bool $delete_old_session = false ] )
the function returns a boolean
the function takes an optional boolean parameter, the value will be false if one isn’t provided.

1 Like

So it means that

is “string session_id” a function?
or
is “session_id” a function?

(I need how to read the manual first since I am not a english native speaker I don’t have intuitive sense.)

The “string” before the function name (or bool, array, mixed, etc. as the case may be) is the data type the function returns, not part of the function name.

[quote=“Mittineague, post:8, topic:226165, full:true”]
The “string” before the function name (or bool, array, mixed, etc. as the case may be) is the data type the function returns, not part of the function name.
[/quote] Now I understand “string” means data type. and “session_id” is a function name.

[quote=“Mittineague, post:8, topic:226165, full:true”]
string session_id ([ string $id ] )
the function returns a string[/quote]
I like to ask you about the word returns.

The code below is the all code at http://dot.kr/Q/function.php

[code]<?php
session_id(‘hello1’);
session_start();

$sessid=session_id();

echo $sessid;
?>[/code]I can say it produces “hello1” or it echos “hello1”.

returns means “produces” or “echos”?

[quote=“Mittineague, post:8, topic:226165, full:true”]
string session_id ([ string $id ] )
the function takes an optional string parameter[/quote]
So is [ string $id ] an optional string parameter?

I think “string” in [ string $id ] is a data type.

Does “” in [ string $id ] mean optional?

in order to make my question more clearly and show you what I am trying, I suggest the link below.

http://dot.kr/Q/php/session/new_start/01.php

http://dot.kr/Q/php/session/read/01.php

http://dot.kr/Q/php/session/regenerate/01.php

Funny, but as simple as “return” is, I have some trouble coming up with a good explanation.

Some functions “do stuff” and don’t return anything (though IMHO it’s usually a good idea to whether or not it will be used). and you might see “void”.

But usually they do something. eg. add numbers together, change letters to uppercase, validate an email address, etc. etc.
For these, usually whatever the function does, with whatever is passed to it, is wanted so it can be used. So after the function does its thing it returns the change or at least indicates success.

yes, in the PHP documentation’s “synopsis” / “description” square brackets are used to signify optional parameters.

The code below is the main code at http://dot.kr/Q/php/session/new_start/01.php

[code]<?php
session_id(‘hello1’);
session_start();

$old_sessionid = session_id();

session_id(‘hello2’);
session_start();

$new_sessionid = session_id();

echo “Old Session: $old_sessionid
”;
echo “New Session: $new_sessionid
”;
?>[/code]

And the code below is the main code at http://dot.kr/Q/php/session/read/01.php

[code]<?php
session_start();
$sessionid = session_id();

echo '$sessionid;
?>[/code]

And the code below is the main code at http://dot.kr/Q/php/session/regenerate/01.php

[code]<?php
session_id(‘hello1’);
session_start();

$old_sessionid = session_id();

session_regenerate_id (‘hello2’);

$new_sessionid = session_id();

echo “Old Session: $old_sessionid
”;
echo “New Session: $new_sessionid
”;
?>[/code]

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.