简体   繁体   中英

How to use same randomly generated value in header.php and footer.php in wordpress

I want to use same randomly generated value in header.php and footer.php in wordpress. i have try this function to randomly generated value:

function randValue() {
$randCheaters = substr(md5(microtime()),rand(0,26),5);
return $randCheaters;
}

In header.php i use some thing like this

<header id="<? echo randValue(); ?>">

and in footer.php i have used this

<footer id="<? echo randValue(); ?>">

The problem is it generate different value in footer and header, how can i get the same value in both place.

How about rather than generating the random number each time through a function call, you do it once at the start of your page, then store it in a variable which you later call.

//No function, code runs automatically
$randCheaters = substr(md5(microtime()),rand(0,26),5);  //Variable $randCheaters now contains a random value

Now echo $randomCheaters into both ids, and they will be the same

<header id="<? echo $randCheaters; ?>">
<footer id="<? echo $randCheaters; ?>">

You have to define the variable before echoing in any place. From your question it seems that the random code should be placed in header. As you know header section may comes just after html tag. So its better you assign the variable before html tag.

<?php $randCheaters = substr(md5(microtime()),rand(0,26),5); ?> <html>

We dont need function to be called. Now echo in id= attribute under header and footer.

If you want to use it for every page then assign the rand value to a session variable.

`$_SESSION['randCheaters']=$randCheaters;`

Then use this session variable by just echoing.

Posting from mobile phone. Editing option is limited. Sorry for wrong alignments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM